开关控件无法控制它的大小,只能控制它的位置坐标
tag 留个门,为了别的地方能够捡回来接着用 捡回来的时候要用self.window UISwitch *sww1 =(UISwitch *)[self.window viewWithTag:10];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//开关1
UISwitch *sw1 = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 1, 1)];
[sw1 addTarget:self action:@selector(act:) forControlEvents:UIControlEventTouchUpInside];
sw1.tag=10;
[self.window addSubview:sw1];
//开关2
UISwitch *sw2 = [[UISwitch alloc]initWithFrame:CGRectMake(80, 20, 1, 1)];
sw2.tag=20;
[sw2 addTarget:self action:@selector(act:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sw2];
//初始状态为打开
sw1.on=YES;
sw2.on=YES;
[self.window makeKeyAndVisible];
return YES;
}
-(void)act:(UISwitch *)sender
{
BOOL b = sender.on;
//通过tag把开关捡回来
UISwitch *sww1 =(UISwitch *)[self.window viewWithTag:10];
UISwitch *sww2 =(UISwitch *)[self.window viewWithTag:20];
sww1.on=b;
sww2.on=b;
if (b) {
self.window.backgroundColor=[UIColor greenColor];
}
else
{
self.window.backgroundColor=[UIColor redColor];
}
}