iOS开发(8)UISwitch
? UISwitch是iOS中的開關控件。
?//創建UISwitch
? ? UISwitch *switchCtrl = [[UISwitch alloc] init];
? ? //設置frame
? ? switchCtrl.frame = CGRectMake(10, 10, 300, 20);
? ? [self.view addSubview:switchCtrl];
? ? //設置開關狀態
? ? switchCtrl.on = YES;
? ? //設置開的顏色
? ? switchCtrl.onTintColor = [UIColor redColor];
? ? //也可以用圖片
? ? switchCtrl.offImage = [UIImage imageNamed:@"map_temp.png"];
? ? switchCtrl.onImage = [UIImage imageNamed:@"map_temp.png"];
? ? //設置中間圓點的顏色
? ? switchCtrl.thumbTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"skype.png"]];
? ? switchCtrl.tintColor = [UIColor blackColor];
? ? //給switch添加事件
? ? [switchCtrl addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
- (void)test:(UISwitch*)sender
{
? ? NSLog(@"%d",sender.on);
//可以通過判斷sender.on的狀態來實現一些邏輯
}
總結
以上是生活随笔為你收集整理的iOS开发(8)UISwitch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS开发(7)通过uiimageVie
- 下一篇: iOS开发(9)UISlider