【发布时间】:2013-02-06 20:22:27
【问题描述】:
目标是无论我处于纵向还是横向模式,我的图像都将位于距离超级视图的左边缘 20 处,而 viewHolder 也将位于距离超级视图的底部边缘 20 处。
我正在做的是
ViewController.h
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalConsImage;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIView *viewHolder;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomConsViewHolder;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConsViewHolder;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view removeConstraint:self.verticalConsImage];
[self.view removeConstraint:self.topConsViewHolder];
[self.view removeConstraint:self.bottomConsViewHolder];
//the position of the imageView left edge is equal of the superview’s left edge plus 20.
self.verticalConsImage = [NSLayoutConstraint
constraintWithItem:self.imageView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:20.0f];
// add it again to the view
[self.view addConstraint:self.verticalConsImage];
//the position of the viewHolder's bottom edge is equal of the superview’s bottom edge plus 20.
self.bottomConsViewHolder = [NSLayoutConstraint
constraintWithItem:self.viewHolder
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:20.0f];
// add it again to the view
[self.view addConstraint:self.bottomConsViewHolder];
但是,当我运行应用程序时,viewHolder 根本不会以纵向或横向显示。图片附在下面
我在这里缺少什么,如果您对此有任何想法,请提供帮助。谢谢
【问题讨论】:
-
你为什么要在代码中这样做?如果您在 IB 的底部和左侧设置约束的视图持有者,它将保持在那里旋转。无需在代码中执行此操作。我不知道这与我回答的您的其他问题有何关系。如果您希望事物保持固定距离,请在 IB 中执行,如果您需要它们以某种方式移动,您应该像我之前回答的那样在代码中执行。
标签: iphone ios autolayout