容易忘记的易错点

1. @property属性中如果是copy属性要记得:

注意:
在重写setter方法时要记得加上

_name = [name copy]

在内部的init方法中 也要记得加copy,即在内部直接给属性变量赋值时记得加copy

    //这里尽量不要用self.实例变量 而用 _实例变量
- initWithSpId:(SInt32)spId spName:(NSString *)spName type:(SInt32)spType spPinyin:(NSString *)spPinyin spLetter:(NSString *)spLetter {
    if (self = [super init]) {
        _spId = spId;
        _spName = [spName copy];
        _type = spType;
    }
    return self;
}

2. 注意在修改navigation时是只修改了navigationItem还是修改了NavigationBar

如果是仅仅修改了navigationItem那就只对本控制器有效 push或者pop了都不影响,一般有以下几种情况:

  1. 修改了navigationItem的元素

    self.navigationItem.title = @"title";
    self.navigationItem.titleView = [[UIView alloc] init];
    
  2. 修改了navigationItem的BarButtonItem

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popViewController) image:@"general_arrow_back_white" highImage:@"general_arrow_back_orange"];
    

    一般我们可以通过这种方法拦截返回点击事件,比如全局控制器的返回按键已经写好,而我们想要拦截点击事件就可以用这种方法,写一个同一样式的按键触发不同的事件

这些修改是不影响的整个navigationBar的仅此不需要在视图控制器消失时做额外的操作

而如果是修改了navigationBar 那就对全局有影响了,需要在控制器消失时做额外还原处理,一般情况如下:
//修改背景图片
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//修改背景颜色
[self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
//修改navigationBar的整体字体颜色
[self.navigationController.navigationBar setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
 //隐藏navigationBar
self.navigationController.navigationBar.hidden = YES;

此时我们需要在退出时做一下还原操作

[super viewWillDisappear:animated];
[self.navigationController.navigationBar lt_reset];
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,nil]];
self.navigationController.navigationBar.hidden = NO;

3. 隐藏tabbar

我们在某些界面需要隐藏Tabbar的时候,会出现原区域无法响应点击事件的问题。系统的Tabbar已经被我们hidden了,但是仍然无法响应事件,此时我们只要将系统的Tabbar的frame设置为CGRectZero即可。

[UIApplication sharedApplication].keyWindow.rootViewController).tabBar.frame 
= CGRectZero;

4. 自动布局的动画

用`xib`拉的视图或者`masonry`布局的视图在`[UIView animation...]`动画方法中都是可以直接改变`frame`或者`transform`来进行动画的
我们可以直接改变`frame`包括宽、高、中心等 直接进行各种动画,而不用担心因为`autolayout`问题,毕竟masony或者xib拉约束都只是布局问题,在运行时还是会转成`frame`的.

而通常我们拖约束线出去是为了:
1. 在视图还没加载时用代码设置某个约束值  
2. 真的是要改变某个约束值做动画(通常某个视图也和这个约束有关系)

让UIButton的title对齐方式

有些时候我们想让UIButton的title居左对齐

button.textLabel.textAlignment = UITextAlignmentLeft //是没有作用的,我们需要设置

button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; //显示居左
**除了水平 还有垂直方向的对齐方式