UISwitch(開關控制項)、UISegmentedControl(分段控制項)

來源:互聯網
上載者:User

標籤:

一、UISwitch1、初始化UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)]; 2、設定相關屬性s1.onTintColor = [UIColor blueColor];s1.tintColor = [UIColor greenColor];s1.thumbTintColor = [UIColor redColor];3、設定開關狀態s1.on = YES;4、添加事件    [s1 addTarget:self action:@selector(myswitch) forControlEvents:UIControlEventValueChanged];
       [s1 addTarget:self action:@selector(myswitch:) forControlEvents:UIControlEventValueChanged]; 5、加入父視圖  [self.view addSubview:s1]; 添加事件的方法   -(void)myswitch{
    NSLog(@"觸發了改方法");
}
-(void)myswitch:(UISwitch *)s{
    UISwitch *s2 = (UISwitch *)[self.view viewWithTag:1];
    NSLog(@"%d",s.isOn);
    NSLog(@"%d",s2.isOn);}  二、UISegmentedControl1、初始化(1)方式1UISegmentedControl *seg  =[[UISegmentedControl alloc] initWithFrame:CGRectMake(30, 100, 200, 40)];//建立時初始化大小,但是不設定每段的參數(2)方式2UISegmentedControl *seg1 = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]];//初始化時直接給出初始的段數及title2、設定相關屬性(1)[seg setTitle:@"我的" forSegmentAtIndex:0];//設定某一段的標題文字 (2)[seg setImage:[UIImage imageNamed:@"lanzuan"] forSegmentAtIndex:0];//設定某一分段的image,注意title和image二者只能存在一個 (3)[seg insertSegmentWithTitle:@"2" atIndex:0 animated:NO];//在下標為0的位置插入一個標題為2的分段,注意下標的合理性 (4)[seg insertSegmentWithImage:[UIImage imageNamed:@"1.png"] atIndex:0 animated:NO];//在下標為0的位置插入一個分段,設定其圖片 (5)seg.tintColor = [UIColor whiteColor];//設定segment的邊框顏色及文字/圖片顏色 (6)[seg removeSegmentAtIndex:0 animated:YES];//移除某一分段 (7)[seg removeAllSegments];//移除所有的分段 (8)seg.numberOfSegments; //移除某一分段 (9)seg.selectedSegmentIndex = 0;//預設選中第幾個選段 (10)NSString *str = [seg titleForSegmentAtIndex:0];//擷取某一段的title (11)UIImage  *image = [seg imageForSegmentAtIndex:0];//擷取某一段的圖片 (12)[seg setWidth:30 forSegmentAtIndex:0];//設定某一段的寬度 (13)[seg setBackgroundImage:[UIImage imageNamed:@"imsend"] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];//設定常態下的背景圖 (14)[seg setBackgroundImage:[UIImage imageNamed:@"logoin_6"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];//設定選中的背景圖 (15)[seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];//綁定監聽事件   UISegmentedControl *segment1 = [[UISegmentedControlalloc]initWithItems:@[@"你好",@"歡迎",@"加入"]];//初始化時直接給出初始的段數及title
    segment1.frame = CGRectMake(90, 240, 200, 50);//初始化大小
  
    [segment1 insertSegmentWithImage:[UIImage imageNamed:@"3.jpg"] atIndex:1 animated:YES];//插入圖片並允許動畫
   
     NSLog(@"段數:%zi",segment1.numberOfSegments);//擷取段數
    [segment1 setTitle:@"我們" forSegmentAtIndex:0];
    NSString *str = [segment1 titleForSegmentAtIndex:1];//擷取圖片標題,注意圖片中插入的位置要對應起來
   
    NSLog(@"圖片標題=%@",str);
    UIImage *image = [segment1 imageForSegmentAtIndex:1];
    NSLog(@"圖片=%@",image);
   
    //[segment1 setWidth:100 forSegmentAtIndex:0];//設定某段的寬
    //[segment1 setEnabled:NO forSegmentAtIndex:0];//設定某一段是否被啟用
    //[segment1 setEnabled:NO];//整體不被啟用
   
    segment1.selectedSegmentIndex = 1;//設定預設選中項
    NSLog(@"%zi",segment1.selectedSegmentIndex);//擷取當前被選中的段
    segment1.tintColor = [UIColor greenColor];//邊框和選中顏色
    [segment1 setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    // UIControlStateNormal、UIBarMetricsDefault 設定成預設的就好    [segment1 addTarget:self action:@selector(segmentchange) forControlEvents:UIControlEventValueChanged]; //觸發事件-(void)segmentchange{
    NSLog(@"分段被點擊");}       [self.view addSubview:segment1];    
    UISegmentedControl *s = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 400, 100, 50)];
    [s insertSegmentWithTitle:@"分段" atIndex:3 animated:YES];
    [s insertSegmentWithTitle:@"函數" atIndex:0 animated:YES];//增加    //[s removeSegmentAtIndex:0 animated:YES];//移除    [self.view addSubview:s];   

UISwitch(開關控制項)、UISegmentedControl(分段控制項)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.