您的位置:首页 > 移动开发 > IOS开发

IOS纯手写代码支持旋屏

2013-10-29 22:43 387 查看
不用ib纯手写代码实现旋屏效果,xcode4.6.3,今天试了一下,可以做到,但是代码量会增加,基本思路是:在

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}函数中判断toInterfaceOrientation根据这个判断来重新布局,因为每旋转一下都会进这个方法,具体代码如下,只针对3.5寸屏幕,如果换成4寸又得增加大量代码

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

NSLog(@"11111111");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

UIButton *btn = (UIButton *)[self.view viewWithTag:101+i];

btn.frame = CGRectMake(5+(460-5-100-5)*i, 5+(320 -5-50-5)*j, 100, 50);

[self.view addSubview:btn];

[btn setTitle:@"11111" forState:UIControlStateNormal];

}

}

for (int i = 0 ; i < 3; i++) {

UIButton *btn = (UIButton *)[self.view viewWithTag:201+i];

btn.frame = CGRectMake(5+105*i, 320/2 - 50/2, 100, 50);

[self.view addSubview:btn];

[btn setTitle:@"2222" forState:UIControlStateNormal];

}

}

if (toInterfaceOrientation == UIInterfaceOrientationPortrait)

{

NSLog(@"222222222");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

UIButton *btn = (UIButton *)[self.view viewWithTag:101+i];

btn.frame = CGRectMake(5+(320-5-100-5)*i, 5+(460 -5-50-5)*j, 100, 50);

[self.view addSubview:btn];

[btn setTitle:@"11111" forState:UIControlStateNormal];

}

}

for (int i = 0 ; i < 3; i++) {

UIButton *btn = (UIButton *)[self.view viewWithTag:201+i];

btn.frame = CGRectMake(5+105*i, 460/2 - 50/2, 100, 50);

[self.view addSubview:btn];

[btn setTitle:@"2222" forState:UIControlStateNormal];

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios旋屏布局