Tag: nslayoutconstraint

界面生成器中的“宽度等于高度”约束

我找不到在Interface Builder中创build“方形”约束的方法,意思是“宽度等于高度”。 我想可以通过编程添加这样的约束。 IB有什么我可以做的吗? 也许我只是不明白? 这似乎微不足道,但我找不到它。

使用swift iOS8通过代码将约束设置到Storyboard中的元素

我有一个ViewView的一个ViewController。 我已经在故事板中设置了它。 有没有办法以编程方式设置tableView的约束? 我试图在ViewController中从我的tableView中设置一个IBOutlet,并添加约束条件。 但是这没有用。 这是我的ViewController import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() addTableViewConstraints() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func addTableViewConstraints() { tableView.setTranslatesAutoresizingMaskIntoConstraints(false) var topConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: […]

如何在Swift中为NSLayoutConstraint设置animation?

我的目标是animation一个UIImageView 。 我在viewDidLoad声明了一个NSLayoutConstraint 。 在viewDidLoad ,我使用了这个代码: UIView.animateWithDuration(1, animations: { myConstraint.constant = 100 self.view.layoutIfNeeded() }, completion: {finished in }) 为什么我的形象不移动?

Autolayout:原点和大小应根据宽度和高度因素而改变

这是我需要的一个场景。 我已经在IB(尺寸iPhone4英寸)上放置了一个UIButton,其IB上的初始帧是x:100,y:100,w:100,h:100 。 我想要根据设备大小来改变原点和高度。 例如iPhone6的宽度和高度是375X667,因此宽度因子(375/320 = 1.1718)和高度因子(667/568 = 1.1742) 。 在这个场景中,我想通过乘以宽度因子(1.1718)和原点Y和高度乘以高度因子(1.1782)来更改我的button原点X和宽度。 在这种情况下,我的button应显示在x:100X1.1718,y:100X1.1742,w:100X1.1718,h:100X1.1742 。 iPhone6 +也需要同样的东西。 我可以通过设置这个自动调整掩码来轻松实现。 我怎样才能达到相同的使用自动布局? 我应该遵循什么步骤,我应该适用什么限制? 我以前的这个问题 我附上了一些截图,我需要使用Autolayout(上图中显示的自动修复掩码实现)。 我的尝试: – 第一步: – 在IB上取一个尺寸为100X100的button。 设置其X = 100和Y = 100。 第二步: – 应用这个约束。

如何以编程方式更改或更新NSLayoutConstraint

我已经使用代码以编程方式实现了AutoLayout: – (void)addConstraintWithListNavigationViewController:(UIView *)listViewNavigation y:(CGFloat)y height:(CGFloat)height { //WIDTH_ListTableView = 0.4 //set x = 0; NSLayoutConstraint *constraintToAnimate1 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:0.00 constant:0]; [self.view addConstraint:constraintToAnimate1]; //set y = y; NSLayoutConstraint *constraintToAnimate2 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.00 constant:y]; [self.view addConstraint:constraintToAnimate2]; //set Width = self.view.frame.size.width*0.4 NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual […]

使用自动布局,如何使UIImageView的大小dynamic取决于图像?

我想我的UIImageView增长或缩小取决于它显示的实际图像的大小。 但是我希望它能够保持垂直居中,距离超级观点的前沿10点。 但是,如果我设置这两个约束,它抱怨我没有设置足够的约束来满足自动布局。 对我来说,它似乎完美地描述了我想要的。 我错过了什么?

了解自动布局中的乘数以使用相对定位

我想了解如何利用自动布局来将项目相对于其他视图的百分比定位。 例如,我最近得知,你可以指定一个视图的底部应该比它的超级视图底部高4%,使用这个: self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 0.96, constant: 0)) 这对我来说是有道理的,因为乘数1会在视图的底部alignment,所以通过将乘数改为0.96,可以减less4%。 但是你怎么能在另一个方向做同样的事情呢? 你想指定一个标签的顶部应该从超级视图的顶部开始4%。 如果使用同一行,但将属性更改为.Top ,则意味着它会比superview的顶部高4%(但实际上并不会将其移出屏幕)。 你不能有一个负面的乘数,我不认为,我不相信一个值大于1会做任何事情,当常数是0.那么你怎么得到的设置? 我有同样的问题执行前导和尾随。 拖尾很容易。 如果你想从右边10% self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 0.9, constant: 0)) 这是有道理的,因为你拨回0.1或10%,而不是完全alignment在1.0。 但是你如何领导呢? 我以为你可能能够设置标签的领先相对于视图的尾随,然后设置乘数为0.1。 在我看来,这意味着标签将从最右边开始,然后被拨回90%,因此从左边获得所需的10%。 但事实并非如此,我不知道为什么。 你能解释这是如何工作,如何正确使用乘数来获得这些相对布局? 为了简单起见,假设您想要创build一个标签,该标签具有顶部和底部10%的超级视图的高度,尾部和超过视图宽度的10%。 在纵向的iPhone上,标签的上方和下方都会有更多的填充,比左边和右边的填充还要多(像是按比例绘制): 但是,让我们说在iPad上,这将是一个完美的广场视图。 因此填充将是相同的数量,如下所示: 问题是如何定义这种约束是dynamic的,而不是为常量设置一个固定的值。 我已经知道如何做底线和尾随,但顶尖和领导让我难住。 我希望能够理解如何使用乘数来进行更高级的布局,例如,指定一个标签的顶部应该位于另一个标签底部的10%处,而不是将其设置为固定的常数。

每当我的应用程序启动时,为什么我会在执行“-layoutSubviews”后仍然需要“自动布局”?

由于我添加了下面的代码,每当我的应用程序打开这个UITableViewController它崩溃: self.noArticlesView = [[UIView alloc] init]; self.noArticlesView.translatesAutoresizingMaskIntoConstraints = NO; self.noArticlesView.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1]; [self.view addSubview:self.noArticlesView]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]]; 它给了我这个错误: […]

activateConstraints:和deactivateConstraints:在IB中创build的约束旋转后不保留

新的NSLayoutConstraint方法activateConstraints:和deactivateConstraints:似乎不能正确使用IB创build的约束(它们对代码创build的约束正常工作)。 我创build了一个简单的testing应用程序,其中一个button具有两组约束。 一个已安装的集具有centerX和centerY约束,而另一个已卸载的集具有左上和右约束(常量10)。 button方法切换这些约束集。 这里是代码, @interface ViewController () @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *uninstalledConstraints; @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *installedConstraints; @end @implementation ViewController – (IBAction)switchconstraints:(UIButton *)sender { [NSLayoutConstraint deactivateConstraints:self.installedConstraints]; [NSLayoutConstraint activateConstraints:self.uninstalledConstraints]; } -(void)viewWillLayoutSubviews { NSLog(@"installed: %@ uninstalled: %@", ((NSLayoutConstraint *)self.installedConstraints[0]).active ? @"Active" : @"Inactive", ((NSLayoutConstraint *)self.uninstalledConstraints[0]).active ? @"Active" : @"Inactive"); } 当应用程序启动时,button位于由其安装的约束定义的正确的居中位置。 在button的操作方法中激活/closures后,button会正确移动到新的位置,但是当我将视图旋转到横向时,它会移回到其初始定义的位置(尽pipe日志仍然会将新激活的集合显示为活跃)。 当我旋转回肖像时,button保持在其初始位置(居中在屏幕中),现在日志显示约束的初始设置为激活,而激活的约束为非激活。 问题是,这是一个错误,还是这些方法不应该用IB定义的约束以这种方式工作?

宽度和高度等于它的超级查看使用自动布局编程?

我一直在寻找networking中的很多片段,我仍然无法find我的问题的答案。 我的问题是我有一个scrollView(SV),我想要在scrollview(SV)内以编程方式添加一个button,它的超级视图的scrollView(SV)的宽度和高度是相同的,这样当用户旋转设备button时将会有相同的框架scrollView(SV)的。 如何做NSLayout / NSLayoutConstraint? 谢谢