UIStactView 新特性语法新介绍

来源:互联网 发布:excel数据分行显示 编辑:程序博客网 时间:2024/05/07 23:12

新控件UIStackView 栈视图,类似AppleWatch的Group
* 父类: UIView
* 特点:
1. 只能垂直或水平散列,因此不能完全取代Autolayout
2. 能够快速的按顺序取到子控件
3.批量修改间距更快
4.批量改变对齐方式更快

  应用场景tabar

//连线一个stactView
@property (weak,nonatomic)IBOutlet UIStackView *stackView;

@end

@implementationViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
 
//获取一个slider  取的是stactView的第0(就是第一个)控件
   
UISlider *slider= self.stackView.arrangedSubviews[0];
   
//动画
    [
UIView animateWithDuration:0.5animations:^{
    
//设置值
[slider
setValue:0.9animated:YES];
    }];
   
//设置子stackView
    
UIStackView*subViewstart =self.stackView.arrangedSubviews[1];
   
//取到第二个button坐标为1button
   
UIButton *button = subViewstart.arrangedSubviews[1];
   
//设置位置
    [button
setTitle:@"哈哈哈"forState:UIControlStateNormal];
   
   
}
##. iOS9新语法
. Nullability为空性标记
* Xcode6
.3时推出的
*
类似swift?!,可为空:__nullable/ nullable,不能为空:__nonnull/ nonnull
*
意义:看到nullable,就进行判空处理
*
大部分都为nonnull,有宏定义方便使用:NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END
*编译器层面-没有改变系统的底层,类似ARC-编译期

. Lightweight Generics轻量级泛型
*
可以指定容器类中对象的类型了
NSArray<NSString *> *strings = 
@[@"dong", @“peng”];
NSDictionary<NSString *, NSNumber *> *mapping =
@{@"a":@1,@"b":@2};

意义不用多想就清楚下面的数组中存的是什么,避免了跳转头文件及类型使用错误混乱


__kindof
1. 既明确表明了返回值,又让使用者不必写强转。
- (
__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;


范例:使用时前面基本会使用 UITableViewCell 子类型的指针来接收返回值,所以这个 API 为了让开发者不必每次都蛋疼的写显式强转,把返回值定义成了 id 类型,

   
而这个 API 实际上的意思是返回一个 UITableViewCell  UITableViewCell 子类的实例

2. 再举个带泛型的例子,UIView  subviews 属性被修改成了:
@property (nonatomic,readonly,copy) NSArray<__kindofUIView *> *subviews;

这样,写下面的代码时就没有任何警告了:
UIButton *button = (UIButton *)view.subviews.lastObject;

0 0