/** * 拦截所有push进来的子控制器 * @param viewController 每一次push进来的子控制器 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// if (不是第一个push进来的子控制器) {
if (self.childViewControllers.count >= 1) {
// 左上角的返回
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
[backButton sizeToFit];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
viewController.hidesBottomBarWhenPushed = YES; // 隐藏底部的工具条
}
// super的push方法一定要写到最后面
// 一旦调用super的pushViewController方法,就会创建子控制器viewController的view
// 也就会调用viewController的viewDidLoad方法
[super pushViewController:viewController animated:animated];
}
#pragma mark - <UIGestureRecognizerDelegate>
/** * 每当用户触发[返回手势]时都会调用一次这个方法 * 返回值:返回YES,手势有效; 返回NO,手势失效 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 如果当前显示的是第一个子控制器,就应该禁止掉[返回手势]
// if (self.childViewControllers.count == 1) return NO;
// return YES;
return self.childViewControllers.count > 1; //简化写法
}
一、32bit颜色
1> 颜色组成
1) 由ARGB四个颜色通道组成
2) 每一个颜色通道都占据8bit
3) 每一个颜色通道的取值范围是[0, 255] [0x00, 0xff] [0b00000000, 0b11111111]
2> 颜色的表示格式
1) 16进制格式(HEX格式)
* 绿色 #ff00ff00
* 黄色 #ffffff00
* 白色 #ffffffff
* 黑色 #ff000000
2) ARGB格式
* 绿色 255,0,255,0
* 黄色 255,255,255,0
* 白色 255,255,255,255
* 黑色 255,0,0,0
二、24bit颜色
1> 颜色组成
1) 由RGB三个颜色通道组成
2) 每一个颜色通道都占据8bit
3) 每一个颜色通道的取值范围是[0, 255] [0x00, 0xff] [0b00000000, 0b11111111]
2> 颜色的表示格式
1) 16进制格式(HEX格式)
* 绿色 #00ff00
* 黄色 #ffff00
* 白色 #ffffff
* 黑色 #000000
2) RGB格式
* 绿色 0,255,0
* 黄色 255,255,0
* 白色 255,255,255
* 黑色 0,0,0
三、12bit颜色
1> 颜色组成
1) 由RGB三个颜色通道组成
2) 每一个颜色通道都占据4bit
3) 每一个颜色通道的取值范围是[0, 15] [0x0, 0xf] [0b0000, 0b1111]
2> 颜色的表示格式
1) 16进制格式(HEX格式)
* 绿色 #00ff0
* 黄色 #ff0
* 白色 #fff
* 黑色 #000
2) RGB格式
* 绿色 0,15,0
* 黄色 15,15,0
* 白色 15,15,15
* 黑色 0,0,0
Label文字换行居中
图片设置
图片拉伸(View→Mode)
Text Field设置
登陆按钮做圆角样式
方法一
- (void)viewDidLoad {
[super viewDidLoad];
self.loginButton.layer.cornerRadius = 5;
// self.loginButton.layer.masksToBounds = YES;
self.loginButton.clipsToBounds = YES; // 另一种写法
}
方法二
[self.loginButton setValue:@5 forKeyPath:@"layer.cornerRadius"];
[self.loginButton setValue:@YES forKeyPath:@"layer.masksToBounds"];
方法三
点击控制器view退出键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)awakeFromNib
{
// 文本框的光标颜色
self.tintColor = [UIColor whiteColor];
// 文字颜色
self.textColor = [UIColor whiteColor];
// 设置带有属性的占位文字(富文本)
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor grayColor]}];
}
富文本用法
不可变的属性文字
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
attrs[NSUnderlineStyleAttributeName] = @1;
attrs[NSUnderlineColorAttributeName] = [UIColor redColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attrs];
可变的属性文字
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(1, 1)];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(1, 1)];
self.attributedPlaceholder = string;
图文混排
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
// 第一段:placeholder
NSAttributedString *substring1 = [[NSAttributedString alloc] initWithString:self.placeholder];
[string appendAttributedString:substring1];
// 第二段:图片
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"login_close_icon"];
attachment.bounds = CGRectMake(0, 0, 16, 16);
NSAttributedString *subtring2 = [NSAttributedString attributedStringWithAttachment:attachment];
[string appendAttributedString:subtring2];
// 第三段:哈哈
NSAttributedString *substring3 = [[NSAttributedString alloc] initWithString:@"哈哈"];
[string appendAttributedString:substring3];
self.attributedPlaceholder = string;
重写占位字符方法:
方法一
- (void)drawPlaceholderInRect:(CGRect)rect
{
// 占位文字画在哪个矩形框里面
CGRect placeholderRect = self.bounds;
placeholderRect.origin.y = (self.height - self.font.lineHeight) * 0.5;
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor redColor];
attrs[NSFontAttributeName] = self.font;
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
}
方法二
// 占位文字画在哪个位置
CGPoint point;
point.x = 0;
point.y = (self.height - self.font.lineHeight) * 0.5;
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor redColor];
attrs[NSFontAttributeName] = self.font;
[self.placeholder drawAtPoint:point withAttributes:attrs];
方法三:运行时
什么是运行时(Runtime)?
运行时的作用?
- (void)awakeFromNib
{
// 文本框的光标颜色
self.tintColor = [UIColor whiteColor];
// 文字颜色
self.textColor = [UIColor whiteColor];
// UILabel *placeholderLabel = [self valueForKeyPath:@"placeholderLabel"];
// placeholderLabel.textColor = [UIColor redColor];
[self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
// 成员变量的数量
unsigned int outCount = 0;
// 获得所有的成员变量
Ivar *ivars = class_copyIvarList([UITextField class], &outCount);
// 遍历所有的成员变量
for (int i = 0; i<outCount; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 获得成员变量的名字
NSLog(@"%s", ivar_getName(ivar));
}
// 如果函数名中包含了copy\new\retain\create等字眼,那么这个函数返回的数据就需要手动释放
free(ivars);
}
监听text点击
方法2:通过addTarget,方法最简单
// 通过addTarget监听文本框的开始和结束编辑
[self addTarget:self action:@selector(beginEditing) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEnd];
方法3 :通知
- (void)awakeFromNib {
// 通过通知监听文本框的开始和结束编辑
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
}
// 移除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)beginEditing
{
[self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
}
- (void)endEditing
{
[self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
}
/** * 文本框聚焦时调用(弹出当前文本框对应的键盘时调用) */
- (BOOL)becomeFirstResponder
{
[self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor";
return [super becomeFirstResponder];
}
/** * 文本框失去焦点时调用(隐藏当前文本框对应的键盘时调用) */
- (BOOL)resignFirstResponder
{
[self setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor";
return [super resignFirstResponder];
}
登陆和注册页面类似,2个可以互相切换
- (IBAction)loginOrRegister:(UIButton *)button {
// 修改约束
if (self.leftSpace.constant == 0) {
self.leftSpace.constant = - self.view.width;
button.selected = YES;
// [button setTitle:@"已有帐号?" forState:UIControlStateNormal];
} else {
self.leftSpace.constant = 0;
button.selected = NO;
// [button setTitle:@"注册帐号" forState:UIControlStateNormal];
}
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}