当我在纵向模式下改变模拟器时,第二个UIView重叠在第一个UIView上

时间:2015-11-17 10:42:20

标签: ios objective-c ios-autolayout

您好我是iOS的新手,我在主UIviews上以编程方式创建了两个ViewController,我在UIviews添加了两个按钮Next Back按钮

当我点击Next按钮时,我先将UIview移动到第二个UIview(就像我们将一个视图控制器移动到另一个视图控制器一样)。当我点击Back按钮时,我正在使用UIview动画将第二个UIview推回到第一个UIview

我的主要问题是:当我在纵向模式下更改第一个UIview方向时,第二个UIview在我的第一个UIview上重叠。这是我的主要问题,我不明白为什么会出现这个问题/请帮助我。

我的代码:

#import "ViewController.h"

@interface ViewController (){

    UIView * MyFisrtView;
    UIView * MySecondView;

    UIButton * GoNext;
    UIButton * GoBack;
}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    MyFisrtView = [[UIView alloc] init];
    MyFisrtView.backgroundColor = [UIColor orangeColor];
    MyFisrtView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MyFisrtView];

    MySecondView = [[UIView alloc] init];
    MySecondView.backgroundColor = [UIColor lightGrayColor];
    MySecondView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MySecondView];

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MyFisrtView,MySecondView);

    //Appliying Autolayouts for FirstView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MyFisrtView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MyFisrtView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    //Appliying Autolayouts for SecondView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MySecondView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MySecondView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];


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

    GoBack = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    GoBack.frame = CGRectMake(50, 50, 100, 18);
    [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(248, 0, self.view.frame.size.width, self.view.frame.size.height); // starting visible position

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

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

-(void)GoBack:(id)sender{

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

    [UIView animateWithDuration:0.5f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [MySecondView setFrame:CGRectMake(MyFisrtView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];   // final visible position
                     }
                     completion:nil];
}

@end

2 个答案:

答案 0 :(得分:1)

这是因为screen size的{​​{1}}更改。例如,在landscape to portrait模式下,与portrait模式进行比较时,高度会很大,宽度会很小。

因此,您需要做的是,您必须为landscapeportrait修复单独的高度和宽度,并为不同的方向更改landscapex位置

而且,从您的代码中我没有看到任何更改y。如果您想解决问题,则必须实施orientation delegates

答案 1 :(得分:1)

首先注释掉你的约束并进行如下修改

@interface ViewController (){
    UIView * MyFisrtView;
    UIView * MySecondView;

    UIButton * GoNext;
    UIButton * GoBack;

    BOOL isSecondShown;
}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    MyFisrtView = [[UIView alloc] initWithFrame:self.view.frame];
    MyFisrtView.backgroundColor = [UIColor orangeColor];
    MyFisrtView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MyFisrtView];

    MySecondView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    MySecondView.backgroundColor = [UIColor lightGrayColor];
    MySecondView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MySecondView];


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

    GoBack = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    GoBack.frame = CGRectMake(50, 50, 100, 18);
    [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)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    if(isSecondShown){
    [MyFisrtView setFrame:CGRectMake(MySecondView.frame.origin.x - MySecondView.frame.size.width, MySecondView.frame.origin.y, MySecondView.frame.size.width, MySecondView.frame.size.height)];
    [MySecondView setFrame:self.view.frame];
    }
    else{
        [MyFisrtView setFrame:self.view.frame];
        [MySecondView setFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    }
}

-(void)GoNext:(id)sender{

//    MySecondView.hidden = NO;

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

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

                         [MySecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];   // final visible position
                     }
                     completion:^(BOOL finished){
                         isSecondShown = YES;
                     }];
}

-(void)GoBack:(id)sender{

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

    [MyFisrtView setFrame:CGRectMake(MySecondView.frame.origin.x - MySecondView.frame.size.width, MySecondView.frame.origin.y, MySecondView.frame.size.width, MySecondView.frame.size.height)];
    [UIView animateWithDuration:0.5f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [MySecondView setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];   // final visible position
                         [UIView animateWithDuration:0.5f animations:^{
                             [MyFisrtView setFrame:self.view.frame];
                         }];
                     }
                     completion:^(BOOL finished){
                         isSecondShown = NO;
                     }];
}

@end