Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 自动布局中的视图消失,ios_Iphone_Ios_Autolayout - Fatal编程技术网

Iphone 自动布局中的视图消失,ios

Iphone 自动布局中的视图消失,ios,iphone,ios,autolayout,Iphone,Ios,Autolayout,目标是无论我处于纵向模式还是lanscape模式,我的图像都将位于超级视图左边缘20处,而viewHolder也将位于超级视图下边缘20处 我正在做的是 ViewController.h @property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalConsImage; @property (strong, nonatomic) IBOutlet UIImageView *imageView; @property (stro

目标是无论我处于纵向模式还是lanscape模式,我的图像都将位于超级视图左边缘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];
但是,当我运行应用程序时,无论是在纵向还是在lanscape中,都不会显示viewHolder。图片附在下面


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

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


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

为什么要在代码中这样做?如果在IB中设置了底部和左侧约束的视图保持架,它将在旋转时保持不变。无需在代码中执行此操作。我不知道这和我回答的另一个问题有什么关系。如果你想让事物保持固定的距离,就用IB来做,如果你需要它们以某种方式移动,就应该像我之前回答你的那样用代码来做。