iphone - View 在自动布局中消失,ios

标签 iphone ios autolayout

目标是无论我在纵向还是横向模式下,我的图像都将位于距 super View 左边缘 20 的位置,并且 viewHolder 也将位于距 super View 下边缘 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 根本不会以纵向或横向显示。图片附在下面 enter image description here

我在这里缺少什么,如果您对此有任何想法,请帮助。谢谢

最佳答案

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

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

关于iphone - View 在自动布局中消失,ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738080/

相关文章:

ios - dispatch_sync里面的dispatch_sync导致死锁

IOS/ Storyboard/自动布局 : Center round button under horizontal bar

ios - UILabel 上的 constraint.constant 更改后,父 UIView 未调整大小

iphone - 如何为 coverflow 中的 UIimage 设置 Action ?

ios - 使用 NSUserDefaults 从 NSArray 中保存和检索 - Swift

ios - 运行Xcode程序时出错

ios - 没有连接时,具有后台 session 配置的 NSURLSession 不会返回错误

ios - 在 View 中均匀分布空间,没有边距

ios - UIlabel 在运行时给出了错误的浮点值

ios - iOS 应用程序是否需要 View Controller ?