【问题标题】:a view disappear in autolayout, ios视图在自动布局中消失,ios
【发布时间】: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


【解决方案1】:

您需要从 IB 视图顶部以黄色突出显示的视图中删除约束,并删除所有代码,不需要它。如果系统不让你删除那个约束,那是因为那个视图没有固有大小,所以你需要先给它一个固定的高度,然后再删除顶部约束。

我认为您的视图消失的原因是因为这两个约束是在 IB 中的 4" 屏幕上设置的,而模拟器使用的是 3.5" 屏幕,所以要保持这些约束,唯一可以改变的是视图的高度,可能设置为零或更低。

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2017-11-08
    相关资源
    最近更新 更多