Objective c 以编程方式将UIView向右定位,并在纵向和纵向中保持全高;景观
我有一个平板电脑应用程序,我正在工作,我需要添加一个侧面板,看起来像一个透明的覆盖。将显示两种不同类型的面板,因此我在故事板中列出了我的主视图,并计划从.xib文件中提取面板。每个面板都是从UIView扩展的自定义类 我遇到的问题是如何添加布局约束。我希望面板位于右侧,并以纵向和横向模式填充ipad的高度。 这在故事板中很容易,因为我只需要设置约束,使底部空间=0,尾部空间=0,导航栏的空间=0。我如何通过编程实现这一点 到目前为止,我尝试创建NSLayoutConstraint:Objective c 以编程方式将UIView向右定位,并在纵向和纵向中保持全高;景观,objective-c,ios7,nslayoutconstraint,Objective C,Ios7,Nslayoutconstraint,我有一个平板电脑应用程序,我正在工作,我需要添加一个侧面板,看起来像一个透明的覆盖。将显示两种不同类型的面板,因此我在故事板中列出了我的主视图,并计划从.xib文件中提取面板。每个面板都是从UIView扩展的自定义类 我遇到的问题是如何添加布局约束。我希望面板位于右侧,并以纵向和横向模式填充ipad的高度。 这在故事板中很容易,因为我只需要设置约束,使底部空间=0,尾部空间=0,导航栏的空间=0。我如何通过编程实现这一点 到目前为止,我尝试创建NSLayoutConstraint: NSLayo
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint
constraintWithItem:panel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
[self.view addConstraint:bottomConstraint];
问题是,当我运行它时,我得到了以下错误:
无法同时满足约束。可能至少有一个
以下列表中的约束之一是您不想要的约束。尝试
这:(1)查看每个约束,并尝试找出您需要的约束
不要期待;(2) 查找添加不需要的约束的代码或
约束并修复它。(注意:如果您看到
NSAutoresizingMaskLayoutConstraints如果您不了解,请参阅
指向UIView属性的文档
翻译自动调整大小(从皮肤到肌肉)
&它打破了我的新限制
提前感谢您的帮助
编辑:
我解决了我的问题,以下是其他希望执行类似操作的人的正确代码:
//add panel from nib
HOTPanel *panel = [[[NSBundle mainBundle] loadNibNamed:@"HOTPanel" owner:self options:nil] objectAtIndex:0];
panel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:panel];
//constraints
NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:panel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:425.0f];
[self.view addConstraint:constraint_width];
NSLayoutConstraint *constraint_trailing = [NSLayoutConstraint constraintWithItem:panel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.f];
[self.view addConstraint:constraint_trailing];
NSLayoutConstraint *constraint_top = [NSLayoutConstraint constraintWithItem:panel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f];
[self.view addConstraint:constraint_top];
NSLayoutConstraint *constraint_bottom = [NSLayoutConstraint constraintWithItem:panel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.f];
[self.view addConstraint:constraint_bottom];
发布完整的错误。必须有设置约束的信息。您是否在``面板上将
translatesAutoResizingMaskIntoConstraints
设置为否?谢谢您的帮助,但我刚刚解决了我的问题。谢谢做一个答案,并检查它是否正确。