這個登錄頁麵包含了自適應屏幕的大小,數字用戶登錄鍵盤是數字鍵盤、隱藏鍵盤、隱藏密碼等等。 ViewController.h ViewController.m 運行效果圖: 2016-04-06 22:22:01 ...
這個登錄頁麵包含了自適應屏幕的大小,數字用戶登錄鍵盤是數字鍵盤、隱藏鍵盤、隱藏密碼等等。
ViewController.h
#import <UIKit/UIKit.h> #import "UIViewExt.h" @interface ViewController : UIViewController<UITextFieldDelegate> /** * 背景圖片 */ @property(strong,nonatomic) UIImageView *Imagebackgroud; /** * 用戶名輸入框 */ @property(strong,nonatomic) UITextField *NameTextfild; /** * 密碼輸入框 */ @property(strong,nonatomic) UITextField *PasswordTextfild; /** * 登錄按鈕 */ @property(strong,nonatomic) UIButton *LoginButton; /** * 註冊按鈕 */ @property(strong,nonatomic) UIButton *RegistrationButton; @end
ViewController.m
#import "ViewController.h" #define WIDTH self.view.width #define HEIGHT self.view.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setBackgroudImage]; [self setLoginImage]; [self setTextFiled]; [self setButton]; } #pragma Mark - 設置背景圖的方法 -(void)setBackgroudImage { self.Imagebackgroud=[[UIImageView alloc] initWithFrame:self.view.frame]; [self.Imagebackgroud setImage:[UIImage imageNamed:@"beijing"]]; [self.view addSubview:self.Imagebackgroud]; } #pragma Mark - 設置輸入框 -(void)setTextFiled { // 用戶名輸入框設置 self.NameTextfild=[[UITextField alloc] initWithFrame:CGRectMake(WIDTH *0.1, HEIGHT *0.2, WIDTH*0.8, HEIGHT*0.05)]; self.NameTextfild.backgroundColor=[UIColor clearColor]; self.NameTextfild.placeholder=@"請輸入手機號"; // 設置輸入鍵盤為數字鍵盤 self.NameTextfild.keyboardType=UIKeyboardTypeNumberPad; // 設置輸入框內的圖標 預設為不顯示 self.NameTextfild.leftView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phoneIcon"]]; self.NameTextfild.leftViewMode=UITextFieldViewModeAlways; [self.view addSubview:self.NameTextfild]; // 設置用戶名輸入框下的線 UIView *Nameview=[[UIView alloc]initWithFrame:CGRectMake(WIDTH *0.1, HEIGHT *0.2+HEIGHT*0.05,WIDTH*0.8, 1)]; Nameview.backgroundColor=[UIColor whiteColor]; [self.view addSubview:Nameview]; // 密碼輸入框設置 self.PasswordTextfild=[[UITextField alloc] initWithFrame:CGRectMake(WIDTH *0.1,HEIGHT*0.2+HEIGHT*0.06, WIDTH*0.8, HEIGHT*0.05)]; self.PasswordTextfild.backgroundColor=[UIColor clearColor]; self.PasswordTextfild.placeholder=@"請輸入密碼"; // 設置輸入密碼保護(隱藏密碼) self.PasswordTextfild.secureTextEntry=YES; // 設置單擊return隱藏鍵盤需要代理 self.PasswordTextfild.delegate=self; // 設置輸入框內的圖標 預設為不顯示 self.PasswordTextfild.leftView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"passwordIcon"]]; self.PasswordTextfild.leftViewMode=UITextFieldViewModeAlways; [self.view addSubview:self.PasswordTextfild]; // 設置密碼輸入框下的線 UIView *Passwordview=[[UIView alloc]initWithFrame:CGRectMake(WIDTH *0.1,HEIGHT*0.31,WIDTH*0.8, 1)]; Passwordview.backgroundColor=[UIColor whiteColor]; [self.view addSubview:Passwordview]; } #pragma Mark -按鈕設置方法 -(void)setButton { // 登錄按鈕設置 self.LoginButton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH *0.1, HEIGHT*0.35, WIDTH *0.8, HEIGHT * 0.08)]; [self.LoginButton setBackgroundImage:[UIImage imageNamed:@"loginButton"] forState:UIControlStateNormal]; [self.LoginButton setTitle:@"登錄" forState:UIControlStateNormal]; [self.view addSubview:self.LoginButton]; // 註冊按鈕設置 self.RegistrationButton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH *0.1, HEIGHT * 0.45, WIDTH *0.8, HEIGHT * 0.08)]; [self.RegistrationButton setBackgroundImage:[UIImage imageNamed:@"rigisterButton"] forState:UIControlStateNormal]; [self.RegistrationButton setTitle:@"註冊" forState:UIControlStateNormal]; [self.RegistrationButton setTitleColor:[UIColor colorWithRed:0.115 green:0.749 blue:0.769 alpha:1.000] forState:UIControlStateNormal]; [self.view addSubview:self.RegistrationButton]; } #pragma Mark - logo圖片設置 -(void)setLoginImage { // 歡迎圖片設置 UIImageView *welcomeView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"welcome"]]; welcomeView.frame=CGRectMake(WIDTH *0.1, HEIGHT*0.05, WIDTH *0.8, HEIGHT *0.08); [self.view addSubview:welcomeView]; // logo圖片設置 UIImageView *LogoView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; LogoView.frame=CGRectMake(WIDTH*0.4, HEIGHT-HEIGHT*0.08, WIDTH*0.2, HEIGHT*0.03); [self.view addSubview:LogoView]; } #pragma Mark - 設置鍵盤的隱藏 /** * 單擊空白處隱藏鍵盤 * * @param touches 空白處 * @param event 單擊 */ -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if ([self.NameTextfild isFirstResponder]||[self.PasswordTextfild isFirstResponder]) { [self.NameTextfild resignFirstResponder]; [self.PasswordTextfild resignFirstResponder]; } } /** * 單擊return隱藏鍵盤 */ -(BOOL)textFieldShouldReturn:(UITextField *)textField { [self.PasswordTextfild resignFirstResponder]; return YES; } ... @end
運行效果圖:
2016-04-06 22:22:01