如何避免使用程序化自动布局进行硬编码
how to avoid hardcoding with programmatic autolayout
我在使用自动版式时遇到问题(我是新手),虽然我的约束按预期运行(一切都水平居中,垂直间距如我所愿),但当我转向横向时,底部按钮消失。
我知道发生这种情况是因为我已经根据纵向视图限制了我的对象,并且当我们移动到横向时高度和宽度值发生变化时这不再适用。我真的不知道如何在改变方向时考虑这些变化。有什么建议吗?
代码和截图如下:
-(void)setConstraints {
[self.view removeConstraints:self.view.constraints];
UIButton *cameraButton = self.cameraButton;
UILabel *camera = self.videoLabel;
UIButton *libraryButton = self.libraryButton;
UILabel *library = self.libraryLabel;
NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);
NSDictionary *metrics = @{@"horizontalSpacing":@500.0, @"verticalSpacing":@125};
//set up top button to be horizontally centered
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
options:0
metrics:nil
views:views];
//set up top button vertical from top of superview
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
options:0
metrics:nil
views:views]];
//set up top button label to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
options:0
metrics:nil
views:views]];
//set up second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
options:0
metrics:nil
views:views]];
//set up label for second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
options:0
metrics:nil
views:views]];
//set up vertical constraints by spacing ALL objects appropriately
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
options:0
metrics:metrics
views:views]];
self.libraryLabel.textAlignment = NSTextAlignmentCenter;
self.videoLabel.textAlignment = NSTextAlignmentCenter;
self.libraryLabel.backgroundColor = [UIColor redColor];
[self.view addConstraints:constraints];
}
您得到的正是您所要求的。如果这不是您想要的,请不要要求!
想想你的垂直约束:
@"V:|-175-[cameraButton]"
@"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
从上到下构成了一个单一的约束链。当然,如果屏幕 比你的 175 短 加上你的 verticalSpacing
加上其他视图的尺寸和最小间距,底部视图将离开底部屏幕。
如果这不是您想要的,请更改您的设计,这样就不是您想要的了。例如,将您的一些视图从上到下放置,而将一些视图从下到上放置。或者允许你的一些空间改变为父视图高度的百分比。
我在使用自动版式时遇到问题(我是新手),虽然我的约束按预期运行(一切都水平居中,垂直间距如我所愿),但当我转向横向时,底部按钮消失。
我知道发生这种情况是因为我已经根据纵向视图限制了我的对象,并且当我们移动到横向时高度和宽度值发生变化时这不再适用。我真的不知道如何在改变方向时考虑这些变化。有什么建议吗?
代码和截图如下:
-(void)setConstraints {
[self.view removeConstraints:self.view.constraints];
UIButton *cameraButton = self.cameraButton;
UILabel *camera = self.videoLabel;
UIButton *libraryButton = self.libraryButton;
UILabel *library = self.libraryLabel;
NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);
NSDictionary *metrics = @{@"horizontalSpacing":@500.0, @"verticalSpacing":@125};
//set up top button to be horizontally centered
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
options:0
metrics:nil
views:views];
//set up top button vertical from top of superview
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
options:0
metrics:nil
views:views]];
//set up top button label to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
options:0
metrics:nil
views:views]];
//set up second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
options:0
metrics:nil
views:views]];
//set up label for second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
options:0
metrics:nil
views:views]];
//set up vertical constraints by spacing ALL objects appropriately
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
options:0
metrics:metrics
views:views]];
self.libraryLabel.textAlignment = NSTextAlignmentCenter;
self.videoLabel.textAlignment = NSTextAlignmentCenter;
self.libraryLabel.backgroundColor = [UIColor redColor];
[self.view addConstraints:constraints];
}
您得到的正是您所要求的。如果这不是您想要的,请不要要求!
想想你的垂直约束:
@"V:|-175-[cameraButton]"
@"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
从上到下构成了一个单一的约束链。当然,如果屏幕 比你的 175 短 加上你的 verticalSpacing
加上其他视图的尺寸和最小间距,底部视图将离开底部屏幕。
如果这不是您想要的,请更改您的设计,这样就不是您想要的了。例如,将您的一些视图从上到下放置,而将一些视图从下到上放置。或者允许你的一些空间改变为父视图高度的百分比。