话说今天没事,拿来AutoLayout开发秘籍书看起来。书中描述了AutoLayout的内容还是挺深奥一点。特写了个小例子玩玩。
工欲善其事,比有利器。还是第三方的开源库使用起来爽啊!做个记录。权当复习了。
//
// ViewController.m
// AutoLayoutDemo1
//
// Created by jason-lu on 16/1/29.
// Copyright © 2016年 Jason. All rights reserved.
//
#import "ViewController.h"
#import
@interface ViewController ()
@end
@implementation ViewController
@synthesize userLabel;
@synthesize userName;
@synthesize pwdLabel;
@synthesize pwd;
@synthesize login;
@synthesize reset;
- (void)viewDidLoad {
[super viewDidLoad];
[self dract];
}
//初始化界面
- (void)dract {
self.view.backgroundColor =[UIColor whiteColor];
userLabel = [[UILabel alloc]initWithFrame:CGRectZero];
userLabel.text = @"用户名:";
userLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:userLabel];
userName =[[UITextField alloc]initWithFrame:CGRectZero];
userName.translatesAutoresizingMaskIntoConstraints = NO;
userName.placeholder = @"请输入用户名";
userName.borderStyle = UITextBorderStyleRoundedRect;
userName.delegate = self;
[self.view addSubview:userName];
pwdLabel = [[UILabel alloc]initWithFrame:CGRectZero];
pwdLabel.text = @"密 码:";
pwdLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:pwdLabel];
pwd =[[UITextField alloc]initWithFrame:CGRectZero];
pwd.translatesAutoresizingMaskIntoConstraints = NO;
pwd.secureTextEntry = YES;
pwd.placeholder = @"请输入密码";
pwd.delegate = self;
pwd.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:pwd];
//init login button
login = [[UIButton alloc]initWithFrame:CGRectZero];
[login setTitle:@"登陆" forState:UIControlStateNormal];
login.backgroundColor =[UIColor redColor];
[self.view addSubview:login];
login.translatesAutoresizingMaskIntoConstraints = NO;
[login addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
//init reset button
reset = [[UIButton alloc]initWithFrame:CGRectZero];
[reset setTitle:@"重置" forState:UIControlStateNormal];
reset.backgroundColor = [UIColor redColor];
[self.view addSubview:reset];
reset.translatesAutoresizingMaskIntoConstraints = NO;
[reset addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
//设置userLabel的约束条件
NSLayoutConstraint *constraintUserLabel;
constraintUserLabel = [NSLayoutConstraint constraintWithItem:userLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:60];
[self.view addConstraint:constraintUserLabel];
constraintUserLabel = [NSLayoutConstraint constraintWithItem:userLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:80];
[self.view addConstraint:constraintUserLabel];
constraintUserLabel =[NSLayoutConstraint constraintWithItem:userLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:30 ];
[self.view addConstraint:constraintUserLabel];
constraintUserLabel = [NSLayoutConstraint constraintWithItem:userLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:30];
[self.view addConstraint:constraintUserLabel];
//设置用户名输入框的约束条件
NSLayoutConstraint *constraintUserAccount;
constraintUserAccount = [NSLayoutConstraint constraintWithItem:userName attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:150];
[self.view addConstraint:constraintUserAccount];
constraintUserAccount = [NSLayoutConstraint constraintWithItem:userName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30];
[self.view addConstraint:constraintUserAccount];
constraintUserAccount =[NSLayoutConstraint constraintWithItem:userName attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:90];
[self.view addConstraint:constraintUserAccount];
constraintUserAccount = [NSLayoutConstraint constraintWithItem:userName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:55];
[self.view addConstraint:constraintUserAccount];
//设置密码的label的约束条件
NSLayoutConstraint *constraintPwdLabel;
constraintPwdLabel = [NSLayoutConstraint constraintWithItem:pwdLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:60];
[self.view addConstraint:constraintPwdLabel];
constraintPwdLabel = [NSLayoutConstraint constraintWithItem:pwdLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:80];
[self.view addConstraint:constraintPwdLabel];
constraintPwdLabel =[NSLayoutConstraint constraintWithItem:pwdLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:30 ];
[self.view addConstraint:constraintPwdLabel];
constraintPwdLabel = [NSLayoutConstraint constraintWithItem:pwdLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:60];
[self.view addConstraint:constraintPwdLabel];
//设置密码输入框的约束条件
NSLayoutConstraint *constraintPwd;
constraintPwd = [NSLayoutConstraint constraintWithItem:pwd attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:150];
[self.view addConstraint:constraintPwd];
constraintPwd = [NSLayoutConstraint constraintWithItem:pwd attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30];
[self.view addConstraint:constraintPwd];
constraintPwd =[NSLayoutConstraint constraintWithItem:pwd attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:90];
[self.view addConstraint:constraintPwd];
constraintPwd = [NSLayoutConstraint constraintWithItem:pwd attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:90];
[self.view addConstraint:constraintPwd];
//设置登陆按钮的约束条件
NSLayoutConstraint *constraintLogin ;
constraintLogin = [NSLayoutConstraint constraintWithItem:login attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:130];
[self.view addConstraint:constraintLogin];
constraintLogin =[NSLayoutConstraint constraintWithItem:login attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30];
[self.view addConstraint:constraintLogin];
constraintLogin = [NSLayoutConstraint constraintWithItem:login attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:30];
[self.view addConstraint:constraintLogin];
constraintLogin = [NSLayoutConstraint constraintWithItem:login attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:150];
[self.view addConstraint:constraintLogin];
//设置重置按钮的约束条件
NSLayoutConstraint *constraintReset ;
constraintReset = [NSLayoutConstraint constraintWithItem:reset attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:130];
[self.view addConstraint:constraintReset];
constraintReset =[NSLayoutConstraint constraintWithItem:reset attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30];
[self.view addConstraint:constraintReset];
constraintReset = [NSLayoutConstraint constraintWithItem:reset attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:170];
[self.view addConstraint:constraintReset];
constraintReset = [NSLayoutConstraint constraintWithItem:reset attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:150];
[self.view addConstraint:constraintReset];
}
- (void)loginAction:(UIButton *)btn {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"系统提示:" message:@"是否确认登陆系统" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"检测账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *user = userName.text;
NSString*password = pwd.text;
if ([user isEqualToString:@"jason"] && [password isEqualToString:@"123456"]) {
//弹出加载页面
_hud = [[MBProgressHUD alloc] initWithView:self.view];
_hud.mode = MBProgressHUDModeIndeterminate;
_hud.dimBackground = YES;
[self.view addSubview:_hud];
_hud.labelText = @"正在登陆...";
[_hud showAnimated:YES whileExecutingBlock:^{
NSLog(@"%@",@"do somethings....");
[self doTask];
} completionBlock:^{
[_hud removeFromSuperview];
}];
}
}];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alert addAction:okAction];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void) doTask{
//你要进行的一些逻辑操作
sleep(10);
}
- (void)resetAction:(UIButton *)btn {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"系统提示:" message:@"清楚你输入的内容" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *defaultAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[userName becomeFirstResponder];
[pwd becomeFirstResponder];
return YES;
}
@end