繁体   English   中英

如何使用自动布局中心微调器?

[英]How to center a spinner with autolayout?

我在一个项目中使用这个微调器: https//github.com/misterwell/MMMaterialDesignSpinner

如何使用自动布局将其添加到屏幕中央,以便在设备旋转时,它仍然保留在屏幕中央?

您可以通过编程方式添加自动布局约束。 您可以使用4个约束 - 中心x,中心y,宽度和高度:

[self.spinner setTranslatesAutoresizingMaskIntoConstraints:NO];

NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:self.spinner attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:self.spinner attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.spinner attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:kSpinnerWidth];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.spinner attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:kSpinnerHeight];

[self.view addConstraints:@[ centerXConstraint, centerYConstraint, widthConstraint, heightConstraint]];

如果您使用的是故事板,则可以在Container中的Container和Vertical Center中添加Horizo​​ntal Center。 要执行此操作,请单击界面构建器中的视图,然后查看IB屏幕的右下角。 单击4个按钮中的第一个,然后选中相应的框。

如果您正在使用代码,则可以在viewDidLayoutSubviews()编写spinner.center = self.view.center

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM