标签: nslayoutconstraint

如何使用Swift以编程方式添加约束

自上周以来我一直试图解决这个问题,而没有更进一步.好的,所以我需要在Swift中编程方式应用一些约束 来使用这段代码:UIView

var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100));
new_view.backgroundColor = UIColor.redColor();
view.addSubview(new_view);

var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
self.view.addConstraint(constX);

var constY:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);
self.view.addConstraint(constY);

var constW:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0);
self.view.addConstraint(constW);

var constH:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: …
Run Code Online (Sandbox Code Playgroud)

uiview ios autolayout nslayoutconstraint swift

409
推荐指数
14
解决办法
38万
查看次数

setNeedsLayout与setNeedsUpdateConstraints和layoutIfNeeded vs updateConstraintsIfNeeded

我知道汽车布局链基本上包含3个不同的过程.

  1. 更新约束
  2. 布局视图(这里是我们计算框架的地方)
  3. 显示

我不完全清楚的是-setNeedsLayout和之间的内在差异-setNeedsUpdateConstraints.来自Apple Docs:

setNeedsLayout

如果要调整视图子视图的布局,请在应用程序的主线程上调用此方法.此方法记录请求并立即返回.由于此方法不强制立即更新,而是等待下一个更新周期,因此可以在更新任何视图之前使用它来使多个视图的布局无效.此行为允许您将所有布局更新合并到一个更新周期,这通常会提高性能.

setNeedsUpdateConstraints

当自定义视图的属性以会影响约束的方式更改时,可以调用此方法以指示需要在将来的某个时刻更新约束.然后系统将调用updateConstraints作为其正常布局传递的一部分.在需要之前立即更新约束可确保在布局过程之间对视图进行多次更改时,不会不必要地重新计算约束.

当我想在修改约束后为视图设置动画并为我通常调用的更改设置动画:

[UIView animateWithDuration:1.0f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.modifConstrView setNeedsUpdateConstraints];
        [self.modifConstrView layoutIfNeeded];
    } completion:NULL];
Run Code Online (Sandbox Code Playgroud)

我发现,如果我使用-setNeedsLayout的,而不是-setNeedsUpdateConstraints一切都按预期工作,但如果我改变-layoutIfNeeded-updateConstraintsIfNeeded,动画将不会发生.
我试图得出自己的结论:

  • -updateConstraintsIfNeeded 仅更新约束但不强制布局进入该过程,因此仍保留原始帧
  • -setNeedsLayout也叫-updateContraints方法

那么什么时候可以使用一个而不是另一个?关于布局方法,我是否需要在约束或父视图中更改的视图上调用它们?

objective-c ios autolayout nslayoutconstraint

220
推荐指数
2
解决办法
9万
查看次数

UITextView使用自动布局扩展为文本

我有一个完全使用自动布局编程的视图.我在视图中间有一个UITextView,上面和下面有项目.一切正常,但我希望能够在添加文本时扩展UITextView.当它扩展时,它应该将其下面的所有内容都推下来.

我知道如何以"弹簧和支柱"的方式做到这一点,但是有一种自动布局方式吗?我能想到的唯一方法是每次需要增长时删除并重新添加约束.

uitextview ios autolayout nslayoutconstraint

155
推荐指数
8
解决办法
9万
查看次数

无法同时满足约束,将尝试通过违反约束来恢复

以下是我在调试区域收到的错误消息.它运行正常,没有任何错误,除了我收到此错误.这会阻止苹果接受应用吗?我如何解决它?

2012-07-26 01:58:18.621 Rolo[33597:11303] 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:0x887d630 h=--& v=--& V:[UIButtonLabel:0x886ed80(19)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x887d5f0 h=--& …
Run Code Online (Sandbox Code Playgroud)

debugging objective-c ios nslayoutconstraint

140
推荐指数
8
解决办法
13万
查看次数

我可以更改NSLayoutConstraint的乘数属性吗?

我在一个superview中创建了两个视图,然后在视图之间添加了约束:

_indicatorConstrainWidth = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view2 attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
[_indicatorConstrainWidth setPriority:UILayoutPriorityDefaultLow];
_indicatorConstrainHeight = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view2 attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
[_indicatorConstrainHeight setPriority:UILayoutPriorityDefaultLow];
[self addConstraint:_indicatorConstrainWidth];
[self addConstraint:_indicatorConstrainHeight];
Run Code Online (Sandbox Code Playgroud)

现在我想用动画更改乘数属性,但我无法弄清楚如何更改多重属性.(我在头文件NSLayoutConstraint.h中的私有属性中找到了_coefficient,但它是私有的.)

如何更改多重属性?

我的解决方法是删除旧约束并添加具有不同值的新约束multipler.

iphone objective-c ios nslayoutconstraint

135
推荐指数
10
解决办法
7万
查看次数

iOS:如何为新的自动布局约束(高度)设置动画

我以前从未使用过autolayout约束.我有一个小的新应用程序我正在研究并注意到NIB的观点默认为自动布局.所以,我想我会抓住机会它一起努力,并试图找出Apple的目标.

第一个挑战:

我需要调整MKMapView的大小,我想将它设置为新位置.如果我按照以前的方式这样做:

[UIView animateWithDuration:1.2f
     animations:^{
         CGRect theFrame = worldView.frame;
         CGRect newFrame = CGRectMake(theFrame.origin.x, theFrame.origin.y, theFrame.size.width, theFrame.size.height - 170);
         worldView.frame = newFrame;
}];
Run Code Online (Sandbox Code Playgroud)

...然后,只要兄弟视图得到更新,MKMapView就会"快速"回到原来的高度(在我的情况下,正在更新UISegmentedControl的标题[myUISegmentedControl setTitle:newTitle forSegmentAtIndex:0]).

所以,我觉得我想要做的是从等于父视图的HIGHT到可相对于UISegmentedControl的顶部,它改变的MKMapView的限制覆盖:V:[MKMapView]-(16)-[UISegmentedControl]

我想要的是缩短MKMapView高度,以便显示地图视图下方的一些控件.为此,我认为我需要将约束从固定的全尺寸视图更改为底部被约束到UISegmentedControl顶部的约束...并且我希望它在视图缩小到新大小时进行动画处理.

怎么会这样呢?

编辑 - 虽然视图底部立即向上移动170,但此动画不是动画:

    [UIView animateWithDuration:1.2f
         animations:^{
             self.nibMapViewConstraint.constant = -170;

    }];
Run Code Online (Sandbox Code Playgroud)

并且nibMapViewConstraint在IB中连接到底部垂直空间约束.

uiview ios autolayout nslayoutconstraint

122
推荐指数
3
解决办法
8万
查看次数

Autolayout:为superview添加约束而不是Top Layout Guide?

我在故事板中的UIViewController中有一个UIView,我想在空间上添加约束,查看距离上边缘的距离.

现在,当我通常按ctrl +拖动到ViewController的主视图时,我只能选择将其设置为顶部布局指南.

这对我来说是一个问题,因为在应用程序中的某一点我将主视图向上移动大约20-50px然后发生的是我不会移动的视图...因为它不与superview对齐.

如何在故事板中手动执行此操作或者是否必须以编程方式添加它?

我正在使用xcode 6.

storyboard ios autolayout nslayoutconstraint

109
推荐指数
3
解决办法
4万
查看次数

删除视图时,约束会发生什么

我的问题很简单,但我在文档中找不到任何信息.

从视图层次结构中删除视图(或移动到另一个视图)时,布局约束会发生什么?

例如,让我们容器C与子视图AB.容器C有一些约束.然后我们打电话[A removeFromSuperview].约束会发生什么A

那么如果我们增加会发生什么AC着?

ios autolayout nslayoutconstraint

106
推荐指数
2
解决办法
2万
查看次数

NSLayoutConstraint崩溃了ViewController

可能重复:
presentViewController:在iOS 6上崩溃(AutoLayout)

单击我的应用程序中的按钮时出现此错误:

2012-06-28 21:43:36.860 AppName[2403:707] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint'
*** First throw call stack:
(0x3568788f 0x37a2e259 0x35687789 0x356877ab 0x333a254d 0x333a26bb 0x333a2423 0x33333001 0x332a13c7 0x3317ec59 0x330f4c17 0x330ff267 0x330ff1d5 0x3319e59b 0x3319d367 0x331f86a7 0x8fb11 0x355e13fd 0x330d6e07 0x3319c5e7 0x355e13fd 0x330d6e07 0x330d6dc3 0x330d6da1 0x330d6b11 0x330d7449 0x330d592b 0x330d5319 0x330bb695 0x330baf3b 0x3727a22b 0x3565b523 0x3565b4c5 0x3565a313 0x355dd4a5 0x355dd36d 0x37279439 0x330e9cd5 0x8f6cb 0x8f628)
terminate called throwing an exception
Run Code Online (Sandbox Code Playgroud)

错误在这行代码上抱怨:

-(IBAction) goToAbout {
    About *screen = [[ …
Run Code Online (Sandbox Code Playgroud)

ios nslayoutconstraint

105
推荐指数
1
解决办法
3万
查看次数

UITableViewCell中iOS7上的自动布局约束问题

我正在以编程方式使用自动布局约束来布局我的自定义UITableView单元格,并且我正确地定义了单元格大小 tableView:heightForRowAtIndexPath:

它在iOS6上运行得很好,在iOS7中看起来也很好

但是当我在iOS7上运行应用程序时,这是我在控制台中看到的那种消息:

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] 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 …
Run Code Online (Sandbox Code Playgroud)

uitableview ios autolayout nslayoutconstraint ios7

104
推荐指数
4
解决办法
6万
查看次数