我正在构建一个应用程序,我想完全避免使用 Storyboard 和 Interface Builder,因此所有 UI 都应该在代码中指定。我在用纯布局,一个很好的配置 APIAutoLayout
限制。
然而,我的问题是,它看起来像AutoLayout
不使用 Interface Builder 时被禁用。updateViewConstraints
,我根据给出的建议放置布局的方法PureLayout
作者,根本不叫。
只是为了提供有关我的设置的更多信息:
- deleted
Main.storyboard
并从我的中删除了该条目Info.plist
- 手动设置
self.window
in AppDelegate.m
并添加了UINavigationController
with MainMainController
as rootViewController
如前所述,我的主要问题是updateViewConstraints
没有被叫到MainViewController
但 UI 元素全部显示为frame
我在初始化期间传递给他们的。
Note:在我看来,我只需要在某处启用一些标志来模仿 Interface Builder 中的复选框,您可以使用它来指示是否要使用AutoLayout
.
MainViewController.m
@interface MainViewController ()
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, assign) BOOL didSetupConstraints;
@end
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.startButton];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
}
- (UIButton *)startButton
{
if (!_startButton) {
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
CGRect startButtonFrame = CGRectMake(75.0, 75.0, 250.0, 44.0);
startButton.frame = startButtonFrame;
[startButton setTitle:@"Start" forState:UIControlStateNormal];
[startButton setTranslatesAutoresizingMaskIntoConstraints:NO];
_startButton = startButton;
}
return _startButton;
}
- (void)updateViewConstraints
{
NSLog(@"Update view contraints");
if (!self.didSetupConstraints) {
[self.startButton autoCenterInSuperview];
self.didSetupConstraints = YES;
}
[super updateViewConstraints];
}
@end
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)