如何使用自动布局以编程方式将活动指标定位到其超级视图的中心?
为什么下面的代码不能用于将活动指标定位到其超级中心的中心:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [self.mysuperview addSubview:activityIndicator]; [activityIndicator addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(>=20)-[view(==100)]-(>=20)-|" options:NSLayoutFormatAlignAllCenterX | NSLayoutFormatAlignAllCenterY metrics:nil views:@{@"view" : self.mysuperview}]];
活动指示器位于左上angular的某处,绝对不在中心。
==================更新:find解决scheme:我必须在创build指标后closures自动调整约束,然后在给定工作的所有解决scheme:
[activityIndicator setTranslatesAutoresizingMaskIntoConstraints:NO];
我在@Vignesh给出的链接上find了,所以我接受他/她的回答。
你可以试试这个,
UIView *superview = self.mysuperview; NSDictionary *variables = NSDictionaryOfVariableBindings(activityIndicator, superview); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[activityIndicator]" options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables]; [self.view addConstraints:constraints]; constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[activityIndicator]" options: NSLayoutFormatAlignAllCenterY metrics:nil views:variables]; [self.view addConstraints:constraints];
从这里采取。
以下四个Swift 3代码示例演示了如何使用Auto布局将UIView
集中到UIViewController
的UIView
中。
所有样本产生相同的结果,但根据您的需要和口味,您可以select其中一个。
如果你的UIActivityIndicatorView
的superview不是self.view
,你只需要用你自己的(unwrapped) superview
代替每个self.view
调用。
1. NSLayoutConstraint
风格
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) indicatorView.isHidden = false indicatorView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(indicatorView) // Auto layout let horizontalConstraint = NSLayoutConstraint(item: indicatorView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0) let verticalConstraint = NSLayoutConstraint(item: indicatorView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) /* // You can replace NSLayoutConstraint activate(_:) call with the following lines: self.view.addConstraint(horizontalConstraint) self.view.addConstraint(verticalConstraint) */ } }
2. UIViewAutoresizing
风格
在运行时,Spring和Struts将被转换成相应的自动布局约束。
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) indicatorView.isHidden = false indicatorView.translatesAutoresizingMaskIntoConstraints = true // default is true self.view.addSubview(indicatorView) // Springs and struts indicatorView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY) indicatorView.autoresizingMask = [ .flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin ] } }
3.视觉格式语言风格
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) indicatorView.isHidden = false indicatorView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(indicatorView) // Auto layout let views: [String: Any] = ["superview": self.view, "indicatorView": indicatorView] let horizontalConstraints = NSLayoutConstraint .constraints(withVisualFormat: "H:[superview]-(<=0)-[indicatorView]", options: .alignAllCenterY, metrics: nil, views: views) let verticalConstraints = NSLayoutConstraint .constraints(withVisualFormat: "V:[superview]-(<=0)-[indicatorView]", options: .alignAllCenterX, metrics: nil, views: views) self.view.addConstraints(horizontalConstraints) self.view.addConstraints(verticalConstraints) } }
4. NSLayoutAnchor
风格(需要iOS 9)
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) indicatorView.isHidden = false indicatorView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(indicatorView) // Auto layout let horizontalConstraint = indicatorView .centerXAnchor.constraint(equalTo: self.view.centerXAnchor) let verticalConstraint = indicatorView .centerYAnchor.constraint(equalTo: self.view.centerYAnchor) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) /* // You can replace NSLayoutConstraint activate(_:) call with the following lines: self.view.addConstraint(horizontalConstraint) self.view.addConstraint(verticalConstraint) */ } }
它在angular落里满足了它的要求,因为你并不是说每一边的差距都是一样的。 试试这个:
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
并垂直居中做这个:
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
另外,我强烈build议使用FLKAutoLayout项目来简化所有这些:
https://github.com/dkduck/FLKAutoLayout
那你可以这样做:
[activityIndicator alignCenterXWithView:self.superview predicate:nil]; [activityIndicator alignCenterYWithView:self.superview predicate:nil];
哪个好:)
我集中在我的活动指标在其超级视图中,通过在界面生成器中添加一个为零的alpha(在我的视图控制器类IBOutlet)。 然后,我添加约束将其集中在X轴和Y轴。 最后,我添加了宽度和高度约束来消除自动布局错误。
在我的视图控制器的startActivityIndicator方法中,我将活动指示符的alpha设置为1,并在其上调用startAnimating方法。 在我的stopActivityIndicator方法中,我调用了stopAnimating方法,并将活动指示符的alpha值设置为零。
- 如何在UIScrollView中将UIImage视图缩放为一个带有Autolay的正方形
- 是否有可能在UIScrollView放大和缩小UIImageView,但坚持自动布局居中?
- UIScrollView自动布局与“页脚”固定到屏幕底部
- 使用自动布局约束dynamic调整表格视图单元格大小
- 如何在animateTransition期间获取ToViewController中子视图的框架:?
- 使用Autolay的UIScrollViewdynamicUILabel高度
- 在tableHeaderView中使用自动布局
- 如何使用自动布局在UIPageViewController中定位子视图?
- 属性不可用:8.0版本之前的iOS版本的第一个基线布局属性