为什么使用Autolayout/Constraints而不是frame和layoutSubviews?

use*_*872 1 frame ios autolayout

我为我的应用程序的启动屏幕实现了一个视图控制器,如下所示.我决定使用框架而不是autolayout,我想知道是否有任何理由在这里使用autolayout/constraints.

我不允许在我的应用程序上轮换,所以我看不出我会从约束中获得什么好处,因为我不喜欢使用界面构建器,我认为代码更清晰/更容易创建和布局框架.

我感谢任何输入 - 请找到下面的代码.

#import "LaunchViewController.h"
#import "RegisterTableViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface LaunchViewController ()

@property (nonatomic) UILabel *appLabel;
@property (nonatomic) UIButton *signUpButton;
@property (nonatomic) UIButton *loginButton;

@end

@implementation LaunchViewController

#pragma mark - UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor standardBlackColor];
    [self layoutViews];
}

- (void)viewDidAppear {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIView

- (void)layoutViews
{
    self.appLabel.frame = [self _appLabelFrame];
    self.loginButton.frame = [self _loginButtonFrame];
    self.signUpButton.frame = [self _signUpButtonFrame];

    [self.view addSubview:self.appLabel];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.signUpButton];
}

#pragma mark - Layout

- (CGRect)_appLabelFrame
{
    CGFloat x_offset = 0;
    CGFloat y_offset = (self.view.frame.size.height / 10);
    CGFloat width =self.view.frame.size.width;
    CGFloat height = 50;
    return CGRectMake(x_offset, y_offset, width, height);
}

- (CGRect)_signUpButtonFrame
{
    CGFloat height = self.view.frame.size.height/14;
    CGFloat x_offset = self.view.frame.size.width / 24;
    CGFloat y_offset = self.view.frame.size.height - ((height + x_offset));
    CGFloat width = self.view.frame.size.width - (2 * x_offset);
    return CGRectMake(x_offset, y_offset, width, height);}

- (CGRect)_loginButtonFrame
{
    CGFloat height = self.view.frame.size.height/14;
    CGFloat x_offset = self.view.frame.size.width / 24;
    CGFloat y_offset = self.view.frame.size.height - ((2 * height)+(2 * x_offset));
    CGFloat width = self.view.frame.size.width - (2 * x_offset);
    return CGRectMake(x_offset, y_offset, width, height);
}

#pragma mark - Getters and Setters

- (UILabel *)appLabel
{
    if (!_appLabel){
        _appLabel = [[UILabel alloc] init];
        _appLabel.text = @"iOS APP";
        _appLabel.textAlignment = NSTextAlignmentCenter;
        [_appLabel setFont:[UIFont appThinTitleFont]];
        _appLabel.textColor = [UIColor whiteColor];
    }
    return _appLabel;
}

- (UIButton *)signUpButton
{
    if (!_signUpButton){
        _signUpButton = [[UIButton alloc] init];
        _signUpButton.backgroundColor = [UIColor darkBlueColor];
        [_signUpButton setTitle:@"SIGN UP" forState:UIControlStateNormal];
        [_signUpButton.titleLabel setFont:[UIFont largeRegularButtonFont]];
        [_signUpButton addTarget:self action:@selector(signupPageSegue) forControlEvents:UIControlEventTouchUpInside];
    }
    return _signUpButton;
}

- (UIButton *)loginButton
{
    if (!_loginButton){
        _loginButton = [[UIButton alloc] init];
        _loginButton.backgroundColor = [UIColor clearColor];
        _loginButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        _loginButton.layer.borderWidth =1.0f;
        [_loginButton setTitle:@"LOGIN" forState:UIControlStateNormal];
        [_loginButton.titleLabel setFont:[UIFont largeRegularButtonFont]];
        [_loginButton addTarget:self action:@selector(loginPageSegue) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

#pragma mark - Targets

- (void)signupPageSegue
{
    [self performSegueWithIdentifier:@"SignUpSegue" sender:self];
}

- (void)loginPageSegue
{
    [self performSegueWithIdentifier:@"LoginSegue" sender:self];
}

@end
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Dan*_*iel 5

花一些时间学习自动布局.你会很高兴的.

您显示的布局可以在Interface Builder中更加简单地表达,更重要的是,随着新要求的出现,将更容易更新.

虽然这个特定的屏幕确实可行,但确实如此.随着应用程序的增长,您将很快了解到代码中表达所有内容的过于劳动密集型.

另请注意,如果您想在iPad上运行此应用程序,该应用程序将无法支持多任务处理.