【发布时间】:2015-11-19 15:44:47
【问题描述】:
嗨,我是自动布局的新手,在我的项目中,我使用自动布局以编程方式添加了两个 UIView,并且我添加了两个 UIButton,它们是 Next 和 Back 按钮,当我单击 Next 按钮时,我将 MyFirst UIView 推到第二个UIView 使用 UIView 动画,当我单击后退按钮时,我从第二个 UIView 推回到第一个 UIView ok 一切都很好,基于我的代码
但是这里我的主要问题是当我将模拟器中的第一个 UIView 方向从纵向更改为横向时,然后第二个 UIView 与我的第一个 UIView 重叠,我知道这一定是约束问题,我的意思是我必须删除并添加每个约束当我们为此更改方向时,我尝试过下面的代码,但这不起作用,请帮助我,为此我已经尝试了很长时间,但没有人使用约束说出正确的答案
当我们在纵向模式下运行程序时,屏幕就像 image1 一样,当我更改景观模式时,第二个 UIview 重叠在我的第一个 UIview 上,如下面的第二个图像,这是我的主要问题
我的代码:-
#import "SubViewController.h"
@interface SubViewController (){
UIButton * GoNext;
UIButton * GoBack;
NSArray * FHorizental;
NSArray * FVertical;
NSArray * SHorizental;
NSArray * SVertical;
}
@end
@implementation SubViewController
@synthesize MyFisrtView,MySecondView;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"How are you");
[self callAutolayouts];
MyFisrtView.hidden = NO;
MySecondView.hidden = YES;
}
-(void)callAutolayouts{
NSLog(@"Hi");
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];
//Applying autolayouts for MyFirstView and MySecondView
NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MyFisrtView,MySecondView);
//Appliying Autolayouts for FirstView
FHorizental =[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MyFisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary];
FVertical = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MyFisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary];
[self.view addConstraints:FHorizental];
[self.view addConstraints:FVertical];
//Appliying Autolayouts for SEcondView
SHorizental =[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MySecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary];
SVertical = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MySecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary];
[self.view addConstraints:SHorizental];
[self.view addConstraints:SVertical];
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];
}
-(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:^{
[self.view removeConstraints:self.view.constraints];
[self callAutolayouts];
[self.view setNeedsLayout];
[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:^{
[self.view removeConstraints:self.view.constraints];
[self callAutolayouts];
[self.view setNeedsLayout];
[MySecondView setFrame:CGRectMake(MyFisrtView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:nil];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@"portrait");
[self.view removeConstraints:self.view.constraints];
[self callAutolayouts];
[self.view setNeedsLayout];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"LandscapeRight");
[self.view removeConstraints:self.view.constraints];
[self callAutolayouts];
[self.view setNeedsLayout];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"LandscapeLeft");
[self.view removeConstraints:self.view.constraints];
[self callAutolayouts];
[self.view setNeedsLayout];
}
}
@end
图像1:-
图像2:-
【问题讨论】:
-
我也面临同样的问题 AbhiRam 我喜欢这个问题
-
@AbhiRam 我的理解是您只想在纵向模式下显示一个视图(与下一个一起查看),而在横向模式下您想同时显示两个视图(与下一个和后退一起查看)对吗?
-
有两种方法可以摆脱这个问题,使用基本布局 (600,600) 制作您的界面,并使用 IBOutlet 进行高度约束,并将此高度约束连接到文件所有者并将高度约束设为零到视图,当你想显示你的第二个和第一个视图时,你可以改变你的高度约束值,比如假设 IBOutlet NSLayoutConstraint *heightFirstView; [heightFirstView setConstant:0.0f] 最初,当您想要重叠您的第一个视图时,请在适当的委托方法中更改此约束值
-
Jaggen 我的意思是当我单击“下一步”按钮时,我想将第一个 UIView 移动到第二个 UIView,当“返回”按钮我想将第二个 UIView 推回第一个 UIView 时,当我改变时从纵向到景观的方向然后第二个 UIView 重叠在我的第一个 UIView 上,这是这里的主要问题
-
Latika Tiwari 我在这里以编程方式使用自动布局,所以请告诉我我做错了什么
标签: ios objective-c ios-autolayout