如何在Portrait和LandScpae模式下以编程方式修复UIViews

时间:2015-11-20 05:00:46

标签: ios objective-c iphone

您好我是ios的初学者而我ViewController我已经以编程方式添加了两个UIViews他们是“MyFisrtView”和“MySecondView”

我还添加了两个Button,它们是NEXT按钮和BACK按钮,当我单击NEXT按钮时,我使用动画首先将UIView移动到第二个UIView,当我单击BACK按钮时,我返回到第二个UIView ti UIView

这里我的主要问题是当我将模拟器方向改为纵向 LandScpae 时,屏幕分为两部分,就像我的下面图像为什么我遇到这个问题可以任意身体帮助我

我的代码: -

mainViewcontroller: -

#import "MainViewController.h"

@interface MainViewController (){

    UIView * MyMainView;
    SubViewController * Sv;

}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MyMainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    MyMainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    MyMainView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:MyMainView];

    Sv = [[SubViewController alloc]init];
    [Sv createViewView:MyMainView];
}
@end

SubViewClass: -

  #import "SubViewController.h"

    @interface SubViewController (){

        UIButton * GoNext;
        UIButton * GoBack;

        UIView * MyFisrtView;
        UIView * MySecondView;
        UIView * MainView;
    }
    @end

    @implementation SubViewController

    - (void)viewDidLoad {

        [super viewDidLoad];
    }

    -(void)createViewView :(UIView*)mainView{

        MainView = mainView;

        MyFisrtView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height)];
        MyFisrtView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        MyFisrtView.backgroundColor = [UIColor orangeColor];
        [MainView addSubview:MyFisrtView];

        MySecondView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height)];
        MySecondView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        MySecondView.backgroundColor = [UIColor lightGrayColor];
        [MainView addSubview:MySecondView];

        GoNext = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
        [GoNext setTitle:@"Next" forState:UIControlStateNormal];
        [GoNext addTarget:self action:@selector(GoNext:) forControlEvents:UIControlEventTouchUpInside];
        GoNext.backgroundColor = [UIColor lightGrayColor];
        [MyFisrtView addSubview:GoNext];

        GoBack = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
        [GoBack setTitle:@"Back" forState:UIControlStateNormal];
        [GoBack addTarget:self action:@selector(GoBack:) forControlEvents:UIControlEventTouchUpInside];
        GoBack.backgroundColor = [UIColor orangeColor];
        [MySecondView addSubview:GoBack];

        MyFisrtView.hidden = NO;
        MySecondView.hidden = YES;
    }

    -(void)GoNext:(id)sender{

        MySecondView.hidden = NO;

        MySecondView.frame = CGRectMake(MyFisrtView.frame.size.width, 0, MainView.frame.size.width, MainView.frame.size.height);   // starting visible position

        [UIView animateWithDuration:0.5f
                              delay:0.0f
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{

                             [MySecondView setFrame:CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height)];   // final visible position

                         }
                         completion:nil];
    }

    -(void)GoBack:(id)sender{

        MySecondView.frame = CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height);   // starting visible position

        [UIView animateWithDuration:0.5f
                              delay:0.0f
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{

                             [MySecondView setFrame:CGRectMake(MyFisrtView.frame.size.width, 0, MainView.frame.size.width, MainView.frame.size.height)];    // final visible position
                         }
                         completion:nil];
    }

    @end

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
        NSLog(@"portrait");
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"LandscapeRight");

        MyFisrtView.frame = CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height);
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"LandscapeLeft");

        MyFisrtView.frame = CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height);
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

您以编程方式创建视图但不向其添加任何自动布局约束或自动调整大小约束,因此一旦设置myFirstView宽度和高度,如superView宽度,高度为ex。 320和480 ...当你将它改为横向模式时它保持不变...所以要在横向模式下调整它们,你必须再次重置它们的高度和宽度。您可以通过编程方式执行此操作,也可以对它们设置约束。

以编程方式更改其大小,您可以这样做

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationisLandscape(toInterfaceOrientation))
     {
          myFirstView.frame.size.widht = superView.frame.size.height;
          myFirstView.frame.size.height = superView.frame.size.widht;
     }
     else
     {
                  myFirstView.frame.size.widht = superView.frame.size.width;
          myFirstView.frame.size.height = superView.frame.size.height;
     }
}

答案 1 :(得分:0)

如果您正在处理自动布局,请以编程方式为视图添加约束。否则请尝试以下

如果您使用的是iOS 8.0及更早版本,请实施以下方法并更新视图框以进行定位。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    MyFisrtView = CGRectMake(0, 0, MainView.frame.size.width, MainView.frame.size.height);
    // ........ And so on
}

如果您使用的是iOS 9.0及更高版本,请实施相应的方法,并分别更新您的代码

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    // Your code
}