我在IB中设置了多组约束,我想根据某些状态以编程方式在它们之间切换.有一个constraintsA出口集合,所有出口集合都标记为从IB安装,constraintsB出口集合全部在IB中卸载.
我可以编程方式在两个集之间切换,如下所示:
NSLayoutConstraint.deactivateConstraints(constraintsA)
NSLayoutConstraint.activateConstraints(constraintsB)
Run Code Online (Sandbox Code Playgroud)
但是......我无法弄清楚何时这样做.看起来我应该能够做到这一点viewDidLoad,但我不能让它工作.我已经尝试过调用view.updateConstraints()并view.layoutSubviews()在设置约束后,但无济于事.
我确实发现,如果我设置约束,viewDidLayoutSubviews一切都按预期工作.我想我想知道两件事......
我找不到在Interface Builder中创建'square'约束的方法,意思是'width equals height'.我想可以通过编程方式添加这样的约束.我可以在IB做些什么吗?也许我只是看不到它?这似乎微不足道,但我找不到它.
我有一个自定义的UIView子类,它通过一个nib进行初始化.
在-awakeFromNib,我正在创建一个子视图,并试图将其置于超级视图中.
[self setInteralView: [[UIView alloc] init]];
[[self internalView] addConstraint: [NSLayoutConstraint constraintWithItem: [self internalView]
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeCenterX
multiplier: 1
constant: 0]];
Run Code Online (Sandbox Code Playgroud)
这会中断,并导致以下输出:
2013-08-11 17:58:29.628 MyApp[32414:a0b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0xc1dcc80 UIView:0xc132a40.centerX == MyView:0xc1315a0.centerX>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break …Run Code Online (Sandbox Code Playgroud) objective-c ios autolayout uiview-hierarchy nslayoutconstraint
我一直在网上寻找很多片段,我仍然无法找到问题的答案.我的问题是我有一个scrollView(SV),我想以编程方式在scrollView(SV)中添加一个按钮,其superview的相同宽度和高度是scrollView(SV),这样当用户旋转设备按钮时将具有相同的框架scrollView(SV).如何做NSLayout/NSLayoutConstraint?谢谢
我有一个UIView和我使用Xcode Interface Builder设置约束.
现在我需要以UIView's编程方式更新高度常量.
有一个功能myUIView.updateConstraints(),但我不知道如何使用它.
我有一个UITableViewController,如果没有任何显示,它不会显示任何部分.我添加了一个标签,向用户表明此代码无法显示:
label = UILabel(frame: CGRectMake(20, 20, 250, 100))
label.text = "Nothing to show"
self.tableView.addSubview(label)
Run Code Online (Sandbox Code Playgroud)
但现在,我希望它在水平和垂直方向上居中.通常,我会在屏幕截图中选择突出显示的两个选项(加上高度和宽度的选项):

我尝试了以下代码来添加约束但应用程序崩溃时出现错误:
label = UILabel(frame: CGRectMake(20, 20, 250, 100))
label.text = "Nothing to show"
let xConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterY, multiplier: 1, constant: 0)
label.addConstraint(xConstraint)
label.addConstraint(yConstraint)
Run Code Online (Sandbox Code Playgroud)
错误:
When added to a view, the constraint's items must be descendants of that view (or …Run Code Online (Sandbox Code Playgroud) 我刚刚在iOS 6上完成了一个应用程序,并在一周前买了一个开发者帐户,所以没有多少时间玩iOS 7 SDK.刚刚下载了Golden Master版本并尝试升级我的应用程序以与iOS 7兼容.
我收到很多警告说
"标签 - 标签"的错位视图框在运行时会有所不同.
我无法运行该程序.该项目包含tableview,它正在解析和显示来自RSS的XML feed.
如何解决这个问题?
Apple 关于在视图和其中一个布局指南之间创建自动布局约束的文档仅显示了使用VFL的示例.
有没有办法在没有 VFL(使用NSLayoutConstraint其他API或类似的)的情况下以编程方式创建这些约束?
(注意:我特别要求在代码中执行此操作,而不是在Interface Builder中.我不希望length将指南集的计算设置为约束的静态常量,我想要一个约束,其中布局指南的长度发生变化会自动导致约束视图调整位置.)
我已经为我的视图控制器使用了自动布局.我已经在约束中设置了V和H位置,但我想知道当它变为5s,6和6 Plus时如何增加我的按钮大小.这是我为登录按钮添加约束的方式:
NSArray *btncon_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[btnLogin(40)]" options:0 metrics:nil views:viewsDictionary];
[btnLogin addConstraints:btncon_V];
NSArray *btncon_POS_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[btnLogin]-100-|" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:btncon_POS_H];
NSArray *btncon_POS_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[Title]-130-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-10-[btnLogin]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:btncon_POS_V];
Run Code Online (Sandbox Code Playgroud)
但我的问题是,虽然它管理左侧和右侧间隙,但由于高度固定,它在iPhone 6和6 Plus中得到了拉伸.如何根据屏幕尺寸增加尺寸?我认为这可能是宽高比,但我如何在代码中设置宽高比约束?
我所经历和删除每一个用户约束但我仍然收到以下错误ONLY后,我旋转设备.我完全不知道为什么.有没有人有任何想法?
2013-01-14 21:30:31.363 myApp[35869:c07] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>", …Run Code Online (Sandbox Code Playgroud) ios ×9
autolayout ×8
swift ×4
xcode ×2
constraints ×1
dynamic ×1
ios7 ×1
ipad ×1
objective-c ×1
storyboard ×1
uistoryboard ×1
uiview ×1