ViewController.h #import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIImageView *beijing; @property(strong,nonatomic)
ViewController.h
#import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIImageView *beijing; @property(strong,nonatomic) UIImageView *welcome; @property(strong,nonatomic) UIImageView *phoneIcon; @property(strong,nonatomic)UITextField *Intele; @property(strong,nonatomic) UIView *InteleView; @property(strong,nonatomic) UIImageView *passwordIcon; @property(strong,nonatomic) UITextField *Inpassword; @property(strong,nonatomic) UIView *InpasswordView; @property(strong,nonatomic) UIImageView *logo; @property(strong,nonatomic) UIButton *loginButton; @property(strong,nonatomic) UIButton *rigisterButton; @end
ViewController.m
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 添加父視圖beijing背景圖 self.beijing=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // 插入圖片 [self.beijing setImage:[UIImage imageNamed:@"beijing"]]; [self.view addSubview:self.beijing]; // 添加子視圖welcome背景圖 self.welcome=[[UIImageView alloc] initWithFrame:CGRectMake(57, 36, 318, 58)]; // 插入圖片 [self.welcome setImage:[UIImage imageNamed:@"welcome"]]; // 插入子視圖 [self.view addSubview:self.welcome]; // 添加phoneIcon輸入圖標 self.phoneIcon=[[UIImageView alloc] initWithFrame:CGRectMake(66, 122, 25, 30)]; // 插入圖片 [self.phoneIcon setImage:[UIImage imageNamed:@"phoneIcon"]]; [self.view addSubview:self.phoneIcon]; // 添加手機號碼輸入框 self.Intele=[[UITextField alloc] initWithFrame:CGRectMake(99, 122, 267, 40)]; // 設置隱身字 self.Intele.placeholder=@"請輸入手機號碼"; // 設置數字彈出鍵盤 self.Intele.keyboardType=UIKeyboardTypeNumberPad; // 設置無邊框的UITextField self.Intele.borderStyle=UITextBorderStyleNone; [self.view addSubview:self.Intele]; self.InteleView=[[UIView alloc] initWithFrame:CGRectMake(66, 160, 300, 1)]; self.InteleView.backgroundColor=[UIColor whiteColor]; [self.view addSubview:self.InteleView]; // 添加密碼輸入框 self.Inpassword=[[UITextField alloc] initWithFrame:CGRectMake(99, 182, 267, 40)]; self.Inpassword.placeholder=@"請輸入密碼"; self.Inpassword.secureTextEntry=YES; self.Inpassword.borderStyle=UITextBorderStyleNone; [self.view addSubview:self.Inpassword]; self.InpasswordView=[[UIView alloc] initWithFrame:CGRectMake(66, 213, 300, 1)]; self.InpasswordView.backgroundColor=[UIColor whiteColor]; [self.view addSubview:self.InpasswordView]; // 添加passwordIcon輸入圖標 self.passwordIcon=[[UIImageView alloc] initWithFrame:CGRectMake(66, 182, 25, 30)]; [self.passwordIcon setImage:[UIImage imageNamed:@"passwordIcon"]]; [self.view addSubview:self.passwordIcon]; // 添加logo背景圖 self.logo=[[UIImageView alloc] initWithFrame:CGRectMake(173, 694, 69, 22)]; [self.logo setImage:[UIImage imageNamed:@"logo"]]; [self.view addSubview:self.logo]; // 登錄按鈕的相關設置 self.loginButton=[[UIButton alloc] initWithFrame:CGRectMake(66, 282, 318, 50)]; [self.loginButton setBackgroundImage:[UIImage imageNamed:@"loginButton"] forState:UIControlStateNormal]; [self.loginButton setTitle:@"登錄" forState:UIControlStateNormal]; // 事件監聽 [self.loginButton addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.loginButton]; // 註冊按鈕的相關設置 self.rigisterButton=[[UIButton alloc] initWithFrame:CGRectMake(66, 338, 318, 50)]; [self.rigisterButton setBackgroundImage:[UIImage imageNamed:@"rigisterButton"] forState:UIControlStateNormal]; [self.rigisterButton setTitle:@"註冊" forState:UIControlStateNormal]; // 事件監聽 [self.rigisterButton addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventTouchUpInside]; [self.rigisterButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; [self.view addSubview:self.rigisterButton]; } // 點擊空白區域 隱藏鍵盤 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self hideKeyBoard]; } -(void)hideKeyBoard { if ([self.Intele isFirstResponder]||[self.Inpassword isFirstResponder]){ [self.Intele resignFirstResponder]; [self.Inpassword resignFirstResponder]; } }
運行結果: