|
|
//// ViewController.m// HC//// Created by huilinLi on 2025/11/17.//
#import "ViewController.h"#import "HomeViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIImageView *logoImage;@property (nonatomic, strong) UILabel *headLabel;// 账号密码登录@property (nonatomic, strong) UILabel *idLabel;// Homily ID@property (nonatomic, strong) UILabel *pwdLabel;// 密码@property (nonatomic, strong) UITextField *account;// 输入框@property (nonatomic, strong) UITextField *pwd;@property (nonatomic, strong) UIButton *loginButton;// 登录@property (nonatomic, strong) UIButton *agreementButton;// 协议@property (nonatomic, strong) UILabel *agreementLabel;// 协议文字@property (nonatomic, strong) UILabel *loseLabel;// 忘记密码?@property (nonatomic, strong) UILabel *questionLabel;// 登录遇到问题?@property (nonatomic, strong) UIButton *emailButton;// 邮箱登录
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; self.view.userInteractionEnabled = YES;// 开启视图交互 [self setupSubviews]; [self setupConstraints]; NSLog(@"看看导航控制器:%@", self.navigationController ? @"有" : @"哪去了");}#pragma mark - 控件-(void) setupSubviews{ _headLabel = [[UILabel alloc] init]; _headLabel.text = @"账号密码登录"; _headLabel.textColor = [UIColor whiteColor]; _headLabel.font = [UIFont systemFontOfSize:20]; _headLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_headLabel]; // 账号 _idLabel = [[UILabel alloc] init]; _idLabel.text = @"Homily ID | "; _idLabel.textColor = [UIColor whiteColor]; _idLabel.font = [UIFont systemFontOfSize:16]; _idLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_idLabel]; // 密码 _pwdLabel = [[UILabel alloc] init]; _pwdLabel.text = @"密码 | "; _pwdLabel.textColor = [UIColor whiteColor]; _pwdLabel.font = [UIFont systemFontOfSize:16]; _pwdLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_pwdLabel]; // 账号输入 _account = [[UITextField alloc] init]; NSDictionary *placeholder = @{ NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: _account.font }; _account.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入帐号" attributes:placeholder]; _account.textColor = [UIColor whiteColor]; _account.layer.borderWidth = 0; _account.layer.cornerRadius = 5; _account.delegate = self; _account.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_account]; // 密码输入 _pwd = [[UITextField alloc] init]; NSDictionary *placeholderAttrs1 = @{ NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: _pwd.font }; _pwd.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入密码" attributes:placeholderAttrs1]; _pwd.secureTextEntry = YES; _pwd.textColor = [UIColor whiteColor]; _pwd.layer.borderWidth = 0; _pwd.layer.cornerRadius = 5; _pwd.delegate = self; _pwd.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_pwd]; // 登录 _loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_loginButton setTitle:@"登录" forState:UIControlStateNormal]; [_loginButton setTitle:@"这是一个高亮" forState:UIControlStateHighlighted]; [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_loginButton setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted]; _loginButton.backgroundColor = [UIColor blueColor]; _loginButton.layer.cornerRadius = 25;// 圆角是高度一半就是圆形按钮 _loginButton.translatesAutoresizingMaskIntoConstraints = NO; [_loginButton addTarget:self action:@selector(handleLogin) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_loginButton]; // 协议勾选 _agreementButton = [UIButton buttonWithType:UIButtonTypeCustom]; _agreementButton.layer.borderColor = [UIColor whiteColor].CGColor; _agreementButton.layer.borderWidth = 1; _agreementButton.layer.cornerRadius = 3; _agreementButton.translatesAutoresizingMaskIntoConstraints = NO; [_agreementButton addTarget:self action:@selector(agreementButtonSelect:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_agreementButton]; // 协议 _agreementLabel = [[UILabel alloc] init]; _agreementLabel.text = @"我已阅读且同意《注册协议》和《隐私政策》"; _agreementLabel.textColor = [UIColor whiteColor]; _agreementLabel.font = [UIFont systemFontOfSize:12]; _agreementLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_agreementLabel]; // 忘记密码? _loseLabel = [[UILabel alloc] init]; _loseLabel.text = @"忘记密码?"; _loseLabel.textColor = [UIColor blueColor]; _loseLabel.font = [UIFont systemFontOfSize:14]; _loseLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_loseLabel]; // 登录遇到问题? _questionLabel = [[UILabel alloc] init]; _questionLabel.text = @"登录遇到问题?"; _questionLabel.textColor = [UIColor whiteColor]; _questionLabel.font = [UIFont systemFontOfSize:14]; _questionLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_questionLabel]; // 邮箱登录 _emailButton = [UIButton buttonWithConfiguration:[self createEmailButtonConfig] primaryAction:nil]; _emailButton.translatesAutoresizingMaskIntoConstraints = NO;// 禁用自动布局!!!! [self.view addSubview:_emailButton]; // logo的image _logoImage = [[UIImageView alloc] init]; _logoImage.image = [UIImage imageNamed:@"logo"]; _logoImage.contentMode = UIViewContentModeScaleAspectFit; _logoImage.translatesAutoresizingMaskIntoConstraints = NO;// 禁用自动布局!!!! [self.view addSubview:_logoImage]; [self.view bringSubviewToFront:_account]; [self.view bringSubviewToFront:_pwd];}
#pragma mark - 协议选中- (void) agreementButtonSelect:(UIButton *) button { button.selected = !button.selected; if (button.selected) { [button setImage:[UIImage systemImageNamed:@"checkmark"] forState:UIControlStateNormal]; button.tintColor = [UIColor whiteColor]; button.backgroundColor = [UIColor blueColor]; } else { [button setImage:nil forState:UIControlStateNormal]; button.backgroundColor = [UIColor clearColor]; }}#pragma mark - 弹窗提示// 弹窗- (void) showAlertWithMessage:(NSString *)message { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alert animated:YES completion:nil];}
- (void) handleLogin {// NSString *account = _account.text;// if (account.length == 0 || ![account isEqualToString:@"90047681"]) {// [self showAlertWithMessage:@"请输入正确的账号"];// return;// }// // NSString *pwdText = _pwd.text;// if (pwdText.length == 0 || ![pwdText isEqualToString:@"123456"]) {// [self showAlertWithMessage:@"请输入正确的密码"];// return;// }// // if (!_agreementButton.selected) {// [self showAlertWithMessage:@"请勾选协议"];// return;// }
HomeViewController *homeViewController = [[HomeViewController alloc] init]; [self.navigationController pushViewController:homeViewController animated:YES];}
#pragma mark - 约束- (void) setupConstraints { [NSLayoutConstraint activateConstraints:@[ // logo [_logoImage.bottomAnchor constraintEqualToAnchor:_headLabel.topAnchor constant:-30], [_logoImage.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_logoImage.widthAnchor constraintEqualToConstant:50], [_logoImage.heightAnchor constraintEqualToConstant:50], // 标题 顶210,左40,宽200,高20 [_headLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:210], [_headLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_headLabel.widthAnchor constraintEqualToConstant:200], [_headLabel.heightAnchor constraintEqualToConstant:20], // ID 顶310,左40,宽200,高20 [_idLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:310], [_idLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_idLabel.widthAnchor constraintEqualToConstant:200], [_idLabel.heightAnchor constraintEqualToConstant:20], // 密码 ID下60,左40,宽200,高20 [_pwdLabel.topAnchor constraintEqualToAnchor:_idLabel.topAnchor constant:60], [_pwdLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_account.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40], [_pwdLabel.heightAnchor constraintEqualToConstant:20], // 账号输入框:ID上10,左120,宽200,高40 [_account.topAnchor constraintEqualToAnchor:_idLabel.topAnchor constant:-10], [_account.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:120], [_pwd.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40], [_account.heightAnchor constraintEqualToConstant:40], // 密码输入框:密码上10,左120,宽200,高40 [_pwd.topAnchor constraintEqualToAnchor:_pwdLabel.topAnchor constant:-10], [_pwd.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:120], [_pwd.widthAnchor constraintEqualToConstant:200], [_pwd.heightAnchor constraintEqualToConstant:40], // 登录按钮:密码框下80,左右40,高50 [_loginButton.topAnchor constraintEqualToAnchor:_pwd.bottomAnchor constant:60], [_loginButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_loginButton.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40], [_loginButton.heightAnchor constraintEqualToConstant:50], // 协议按钮:登录下70,左40,宽高20 [_agreementButton.topAnchor constraintEqualToAnchor:_loginButton.bottomAnchor constant:30], [_agreementButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40], [_agreementButton.widthAnchor constraintEqualToConstant:20], [_agreementButton.heightAnchor constraintEqualToConstant:20], // 协议:同协议按钮顶,左70,右40 [_agreementLabel.topAnchor constraintEqualToAnchor:_agreementButton.topAnchor], [_agreementLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:70], [_agreementLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40], [_agreementLabel.heightAnchor constraintEqualToConstant:20], // 忘记密码:协议按钮下65,水平居中,高20 [_loseLabel.topAnchor constraintEqualToAnchor:_agreementButton.bottomAnchor constant:65], [_loseLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], [_loseLabel.heightAnchor constraintEqualToConstant:20], // 登录问题:忘记密码下45,水平居中,高20 [_questionLabel.topAnchor constraintEqualToAnchor:_loseLabel.bottomAnchor constant:15], [_questionLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], [_questionLabel.heightAnchor constraintEqualToConstant:20], // 邮箱登录 [_emailButton.topAnchor constraintEqualToAnchor:_loseLabel.bottomAnchor constant:55], [_emailButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], [_emailButton.widthAnchor constraintEqualToConstant:100], [_emailButton.heightAnchor constraintEqualToConstant:100], ]];}// 单写一个- (UIButtonConfiguration *) createEmailButtonConfig { UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration]; config.title = @"邮箱登录"; config.image = [UIImage imageNamed:@"email"]; config.imagePlacement = NSDirectionalRectEdgeTop;// 这里也不对???? config.imagePadding = 8; return config;}#pragma mark - UITextFieldDelegate- (BOOL) textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder];// 点击return键收起键盘 return YES;}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES];// 点空白也收起键盘}
@end
|