#import "ZGKLoginViewController.h"
@interface ZGKLoginViewController ()
// 立即登錄按鈕
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
// 登錄模塊左邊間距
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;
@end
@implementation ZGKLoginViewController
/**** 導航欄顏色 ****/
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (IBAction)closeBtn {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)registerOrLoginBtn:(UIButton *)sender {
// 1.關閉輸入法
[self.view endEditing:YES];
// // 2.改變登錄框的約束,記得改變左邊的約束,不要固定死右邊的約束
if (self.leftMargin.constant) { // 這個里沒有改變控件的frame,只是記錄了要改變的約束,所以不能將這些代碼放入動畫中,要將強制刷新動作放入動畫中
// 目前顯示的是注冊界面,點擊按鈕后切換為登錄界面
self.leftMargin.constant = 0;
sender.selected = NO;
[sender setTitle:@"注冊賬號" forState:UIControlStateNormal];
}else{ // 目前顯示的是登錄界面,點擊按鈕后切換為注冊界面
self.leftMargin.constant = self.view.zgk_width;
sender.selected = YES;
[sender setTitle:@"已有賬號" forState:UIControlStateSelected];
}
// 3.做動畫,強制刷新,或者等runloop也可以
[UIView animateWithDuration:0.25 animations:^{
// 這里會刷新self.view內部所有子控件的布局
// 強制刷新:讓最新設置的約束值馬上作用到UI控件上
[self.view layoutIfNeeded];
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// [self dismissViewControllerAnimated:YES completion:nil];
// 點擊空白地方,關閉輸入法
[self.view endEditing:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 設置圓角登錄按鈕
// 數值前面要加@,包裝成對象,直接屬性用setValue forKey, 間接屬性用setValue forKeyPath
// self.loginBtn.layer.cornerRadius = 5;
[self.loginBtn setValue:@5 forKeyPath:@"layer.cornerRadius"];
// self.loginBtn.layer.masksToBounds = YES;
[self.loginBtn setValue:@YES forKeyPath:@"layer.masksToBounds"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- 0.將登錄窗口的左間距拖入到登錄窗口(注意xib中,右間距不能設置死,最好不設置,不然滑動會出現問題)
// 登錄模塊左邊間距
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;
- 1.點擊注冊或者返回已有賬號按鈕
- (IBAction)registerOrLoginBtn:(UIButton *)sender {
// 1.關閉輸入法
[self.view endEditing:YES];
// 2.改變登錄框的約束,記得改變左邊的約束,不要固定死右邊的約束
if (self.leftMargin.constant) { // 這個里沒有改變控件的frame,只是記錄了要改變的約束,所以不能將這些代碼放入動畫中,要將強制刷新動作放入動畫中
// 目前顯示的是注冊界面,點擊按鈕后切換為登錄界面
self.leftMargin.constant = 0;
sender.selected = NO;
[sender setTitle:@"注冊賬號" forState:UIControlStateNormal];
}else{ // 目前顯示的是登錄界面,點擊按鈕后切換為注冊界面
self.leftMargin.constant = self.view.zgk_width;
sender.selected = YES;
[sender setTitle:@"已有賬號" forState:UIControlStateSelected];
}
// 3.做動畫,強制刷新,或者不強制刷新等runloop也可以
[UIView animateWithDuration:0.25 animations:^{
// 這里會刷新self.view內部所有子控件的布局
// 強制刷新:讓最新設置的約束值馬上作用到UI控件上
[self.view layoutIfNeeded];
}];
}
- 2.讓登錄按鈕的方框角變成圓角
- (void)viewDidLoad {
[super viewDidLoad];
// 設置圓角登錄按鈕
// 數值前面要加@,包裝成對象,直接屬性用setValue forKey, 間接屬性用setValue forKeyPath
// self.loginBtn.layer.cornerRadius = 5;
// 方法二:kvc
[self.loginBtn setValue:@5 forKeyPath:@"layer.cornerRadius"];
// self.loginBtn.layer.masksToBounds = YES;
// 方法二:kvc
[self.loginBtn setValue:@YES forKeyPath:@"layer.masksToBounds"];
}
-
3.xib中設置登錄按鈕的圓角半徑和裁剪
4104104C-877C-431A-927B-156D2915B705.png
- 4.成為第一響應者,并設置占位文字顏色
259D3EB5-7365-41B4-8EC2-17A0CAA84245.png
- 5.報錯: storyboard中設置了錯誤的類
18631315-AF9C-4545-A28F-B0AB2E36384F.png