iOS基础一 UISwitch

来源:互联网 发布:java解决int数值上限 编辑:程序博客网 时间:2024/06/05 23:25
/**     * UISwitch     */            self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];    //self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];    //self.mainSwitch.center = self.view.center;    [self.mainSwitch setOn:YES];    [self.mainSwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:self.mainSwitch];    //off-mod color    self.mainSwitch.tintColor = [UIColor redColor];    //on-mod color    self.mainSwitch.onTintColor = [UIColor brownColor];    //knob's color    self.mainSwitch.thumbTintColor = [UIColor greenColor];    //change the uiimage ios6.0 or later    //self.mainSwitch.onImage = [UIImage imageNamed:@"on"];    //self.mainSwitch.offImage = [UIImage imageNamed:@"off"];

可以在view中配置如上属性,同时target方法如下:

-(void) switchIsChanged: (UISwitch *)paramSender{   // NSLog(@"sender is %@",paramSender);    if([paramSender isOn] == YES)        NSLog(@"the switch is turn on");    else        NSLog(@"the switch is turn off");}

UISwitch 可以修改on/off 图像 要求图片是宽77 高22  且只有ios6之前版本有效

0 0