添加约束而不使用自动布局 [英] Adding constraints without auto-layout

查看:186
本文介绍了添加约束而不使用自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个模式视图包含一个UICollectionView的应用程序,在其下面它是一个视图包含两个按钮(验证/取消)。



我的UICollectionView中的行数可以改变取决于数据,并不知道它的事先,所以我想添加一个约束,总是保持我的按钮30px以下collectionView。



我没有使用自动布局,因为我有一些动画效果更好没有它,所以我不知道如何以编程方式添加这样的约束。 / p>

有没有人知道该怎么办?



非常感谢您的帮助

解决方案

我不确定集合视图。然而,下面的工作与标准视图,并且由于UICollectionView是一种类型的UIView,那么代码也可能与收集视图工作

  NSLayoutConstraint * bottomConstraint = [NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.45 constant:0]; 
NSLayoutConstraint * widthConstraint = [NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:95];
NSLayoutConstraint * heightConstraint = [NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:95];
NSLayoutConstraint * leftConstraint = [NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
[self.view addConstraints:@ [bottomConstraint,widthConstraint,heightConstraint,leftConstraint]];

上面基本上展示了相对于视图的buttonA。但是,要使用它,自动布局应该关闭(如你的情况)。



底部约束说这个约束与按钮A相关,其属性NSLayoutAttributeBottom(底部)和底部将出现在正好0.45 *(底部)。



宽度约束指定它与按钮A相关,并将对其宽度起作用。宽度不依赖于任何其他对象(toItem是nil),它的常量值为95.


I am building an app with a modal view containing a UICollectionView and below it a view containing two buttons (validate/cancel).

The number of rows in my UICollectionView can change depending on the data and don't know it beforehand so I want to add a constraint to always keep my buttons 30px below the collectionView.

I am not using auto-layout for this as I have some animations which work better without it so I don't know how to programmatically add such constraints.

Does anyone have any idea how to do it?

Many thanks for your help

解决方案

I am not sure about the collection view. However, the below works with a standard view and since UICollectionView is a type of UIView, then the code might work with collection view also

NSLayoutConstraint *bottomConstraint=[NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.45 constant:0];
    NSLayoutConstraint *widthConstraint=[NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:95];
    NSLayoutConstraint *heightConstraint=[NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:95];
    NSLayoutConstraint *leftConstraint=[NSLayoutConstraint constraintWithItem:buttonA attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
    [self.view addConstraints:@[bottomConstraint,widthConstraint,heightConstraint,leftConstraint]];

The above basically lays out the buttonA relative to the view. However, to use this, the auto layout should be off (as in your case).

The bottom Constraint says that this constraint is related to button A and will act on its attribute NSLayoutAttributeBottom(bottom side) and the bottom side will be present on exactly 0.45*(Bottom of view).

The width constraint specifies that it is related to button A and will act on its width. The width is not dependent on any other object (toItem is nil) and it will have a constant value of 95.

这篇关于添加约束而不使用自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆