小知识点

1、在由NSString生成NSURL对象时,有可能会出现NSString中包含百分号各类括号冒号等对于url来说的非法字符如果直接进行转换的话将得到nil。在对于复杂url进行转换前,可以先试试对待转换的NSString发送 stringByAddingPercentEscapesUsingEncoding: 将其转换为合法的url字符串(其实目的就是保证非法字符用UTF8编码..) 比如这样:

1
NSString *fixedStr = [reqStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

2、利用代码添加autolayout约束

  • 纯OC代码

    • 在storyboard中的一条约束在代码中的体现就是一个约束对象,所以添加在storyboard上添加一条约束,相当于创建了一个约束对象并将该约束对象添加到对应的视图上
    • 第一步:创建子控件视图
    • 第二步:禁用子控件的autoresizing属性
    • 第三步:创建约束对象
    • 第四步:添加约束对象
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    - (void)viewDidLoad {
    [super viewDidLoad];
    // 1.创建一个子视图,添加到父视图上面
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    #warning 注意点: 如果通过代码来设置Autolayout约束, 那么必须先禁用Autoresizing
    // 2.禁用autoresizing
    // 2.1给需要设置约束的视图禁用autoresizing,禁用父视图autoresizing对子控件无效
    //self.view.translatesAutoresizingMaskIntoConstraints = NO;//错误写法
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    // 3.添加约束
    // 3.1红色(红色距离顶部和左边以及右边的边距固定为20,高度固定为50)
    // 3.1.1顶部(基于父控件)
    /*
    constraintWithItem:需要设置约束的view
    attribute:需要设置约束的位置
    relatedBy:约束的条件
    toItem:约束依赖目标
    attribute:依赖目标约束位置
    multiplier:配置系数
    constant:额外需要添加的长度
    */
    /*
    计算公式:redView.attribute = self.view.attribute * multiplier + constant;
    其中:=符号取决于relatedBy:参数
    typedef NS_ENUM(NSInteger, NSLayoutRelation) {
    NSLayoutRelationLessThanOrEqual = -1, 小于等于
    NSLayoutRelationEqual = 0, 等于
    NSLayoutRelationGreaterThanOrEqual = 1, 大于等于
    };
    */
    // 3.1.1.1创建约束对象
    NSLayoutConstraint *redTopCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
    // 3.1.1.2判断约束条件的层级关系,并添加到对应的视图
    [self.view addConstraint:redTopCos];
    /*
    attribute:传入的是枚举参数
    NSLayoutAttributeLeft = 1, 左边距
    NSLayoutAttributeRight, 右边距
    NSLayoutAttributeTop, 距离顶部边距
    NSLayoutAttributeBottom, 距离底部边距
    NSLayoutAttributeLeading, 左对齐
    NSLayoutAttributeTrailing, 右对齐
    NSLayoutAttributeWidth, 宽度
    NSLayoutAttributeHeight, 高度
    NSLayoutAttributeCenterX, 中点X
    NSLayoutAttributeCenterY, 中点Y
    NSLayoutAttributeBaseline, 文本底线对齐
    */
    // 3.1.2左边约束(基于父控件)
    NSLayoutConstraint *redLeftCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    // 3.1.2.2判断约束条件的层级关系,并添加到对应的视图
    [self.view addConstraint:redLeftCos];
    // 3.1.3右边约束(基于父控件)
    NSLayoutConstraint *redRightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    // 3.1.3.2判断约束条件的层级关系,并添加到对应的视图
    [self.view addConstraint:redRightCos];
    // 3.1.4 高度约束(自身)
    NSLayoutConstraint *redHeightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:50];
    // 3.1.3.2判断约束条件的层级关系,并添加到对应的视图
    [redView addConstraint:redHeightCos];
    }
  • VFL语言实现约束的添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
- (void)viewDidLoad {
[super viewDidLoad];
// 1.创建二个子视图,添加到父视图上面
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
#warning 注意点: 如果通过代码来设置Autolayout约束, 那么必须先禁用Autoresizing
// 2.禁用autoresizing
// 2.1给需要设置约束的视图禁用autoresizing,禁用父视图autoresizing对子控件无效
//self.view.translatesAutoresizingMaskIntoConstraints = NO;//错误写法
redView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
// 3.添加约束
/*
VisualFormat: VFL语句
options: 对齐方式等
metrics: VFL语句中使用到的一些变量
views: VFL语句中使用到的一些控件
*/
// 3.1红色视图
// 水平方向
NSArray *hCos = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-20-|" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(redView)];
[self.view addConstraints:hCos];
//竖直方向
NSArray *vCos = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[redView(==50)]" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(redView)];
[self.view addConstraints:vCos];
// 3.2蓝色视图
// 垂直方向
NSArray *vBlueCos = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView]-20-[blueView(==50)]" options:NSLayoutFormatAlignAllRight metrics:nil views:NSDictionaryOfVariableBindings(redView,blueView)];
[self.view addConstraints:vBlueCos];
// 水平方向
NSLayoutConstraint *hBlueCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
[self.view addConstraint:hBlueCos];
/*VFL格式说明
功能        表达式
水平方向       H:
垂直方向       V:
Views        [view]
SuperView      |
关系         >=,==,<=
空间,间隙       -
优先级        @value
*/
}
  • -(void)addConstraint:(NSLayoutConstraint *)constraint; 用来将约束添加到view。在添加时唯一要注意的是添加的目标view要遵循以下规则:
    • 对于两个同层级view之间的约束关系,添加到他们的父view上
    • 对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上
    • 对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上

>
可以通过-setNeedsUpdateConstraints和-layoutIfNeeded两个方法来刷新约束的改变,使UIView重新布局。这和CoreGraphic的-setNeedsDisplay一套东西是一样的~

具体可以看喵神的AutoLayout(自动布局)入门

3、iOS 11实现左长滑删除cell

1
2
3
4
5
6
7
8
9
10
11
//实现代理方法
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
//删除
UIContextualAction * deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
completionHandler(YES);
}];
deleteRowAction.backgroundColor = [UIColor blueColor];
return [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
}

4.判断编译器的环境:ARC还是MRC?

1
2
3
4
5
6
7
#if __has_feature(objc_arc)
// 当前的编译器环境是ARC
#else
// 当前的编译器环境是MRC
#endif

5.类的初始化方法

1.+(void)load

  • 当某个类第一次装载到OC运行时系统(内存)时,就会调用
  • 程序一启动就会调用
  • 程序运行过程中,只会调用1次

2.+(void)initialize

  • 当某个类第一次被使用时(比如调用了类的某个方法),就会调用
  • 并非程序一启动就会调用

3.在程序运行过程中:1个类中的某个操作,只想执行1次,那么这个操作放到+(void)load方法中最合适