IOS纯手写代码支持旋屏

    不用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旋屏布局)