gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:20:21 25 4
gpt4 key购买 nike

我已经为我的应用程序的启动屏幕实现了一个 View Controller ,如下所示。我决定使用框架而不是自动布局,我想知道是否有任何理由在这里使用自动布局/约束。

我不允许在我的应用程序上旋转,所以我看不到我会从约束中获得什么好处,而且由于我不喜欢使用界面生成器,我认为代码更干净/更容易创建和布局框架.

我感谢任何输入 - 请在下面找到代码。

#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

enter image description here

最佳答案

花一些时间学习自动布局。你会很高兴你做到了。

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

虽然这个特定的屏幕确实可以正常工作。随着应用程序的发展,您将很快了解到,用代码来表达所有内容太费力了。

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

关于ios - 为什么使用 Autolayout/Constraints 而不是 frame 和 layoutSubviews?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com