IOS ——UI篇—— UISegmentedControl的用法總結

來源:互聯網
上載者:User

標籤:

 

 

 

 1  //第一種建立方式 2     UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]]; 3     seg.frame = CGRectMake(40, 100, 60, 20); 4  5     [seg insertSegmentWithImage:[UIImage imageNamed:@"圖片名"] atIndex:4 animated:YES];//給某一段設定圖片(圖片為帶邊框透明的才可以完整顯示,其它圖片不能完整顯示) 6  UIImage *image =  [seg imageForSegmentAtIndex:2];//擷取某一段的圖片 7      NSLog(@"圖片:%@",image); 8      NSString *str = [seg titleForSegmentAtIndex:1];//擷取某一段的標題 9     NSLog(@“第幾個:%zi",segment.selectedSegmentIndex);//擷取當前點擊的編號10      NSLog(@"標題:%@",str);11     [seg setWidth:100 forSegmentAtIndex:1];//設定某一段的寬度12     [seg setEnabled:YES];//設定整體是否被啟用;13     [seg setEnabled:YES forSegmentAtIndex:2];//設定某一段是否啟用14     seg.selectedSegmentIndex = 1;//設定某段為預設選中項15     NSLog(@"%zi",seg.selectedSegmentIndex);//擷取當前被選中的段;16     seg.tintColor = [UIColor purpleColor];//設定邊框和選中顏色;17     [seg setBackgroundImage:[UIImage imageNamed:@"圖片名"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//設定整體的背景圖片18 19     //綁定監聽事件20     [seg addTarget:self action:@selector(方法名:) forControlEvents:UIControlEventValueChanged];21     [self.view addSubview:seg];22 23     //第二種建立方式24     UISegmentedControl *seg2 = [[UISegmentedControl alloc]initWithFrame:CGRectMake(40, 120, 60, 20)];25     [seg2 insertSegmentWithTitle:@"1" atIndex:1 animated:YES];26     [seg2 insertSegmentWithTitle:@"2" atIndex:2 animated:YES];27     [seg2 removeSegmentAtIndex:1 animated:YES];//移除某段28     NSLog(@"%zi",seg2.numberOfSegments);//擷取段數29     [self.view addSubview:seg2];30 31 }32 33 -(void)mySwitch34 {35     UISwitch *swi = (UISwitch *)[self.view viewWithTag:1];36     NSLog(@"%d",swi.isOn);37 38 }

 

IOS ——UI篇—— 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.