zoukankan      html  css  js  c++  java
  • Sagit.Framework For IOS 自动布局教程:9、UI元素的类型转换as

    前言:

    在UI布局的时候,返回的是UIView这个基类型,而设置属性时,需要具体的类型,为了方面性连续性写法,可以用asXXX转换成具体类型。

    类型转换定义:

    @interface UIView(STUIViewAs)
    -(UISwitch*)asSwitch;
    -(UIStepper*)asStepper;
    -(UIProgressView*)asProgressView;
    -(UILabel*)asLabel;
    -(UIImageView*)asImageView;
    -(UITextField*)asTextField;
    -(UITextView*)asTextView;
    -(UIButton*)asButton;
    -(UIScrollView*)asScrollView;
    -(UITableView*)asTableView;
    -(UICollectionView*)asCollectionView;
    -(UIPickerView*)asPickerView;
    -(STView*)asSTView;
    -(UIView*)asBaseView;
    -(UIImage*)asImage;
    @end

    说明:

    1、添加具体UI:返回具体的UI类型。

    [sagit addUIButton:@“name"] 

    此时返回的是具体类型 UIButton。

    这个时候,可以设置一些UIButton的相关属性。

    2、相对布局时:返回的是UIView这个基类型。

    [[sagit addUIButton:@“name"]  x:0 y:0]

    如果此时对UIButton  进行宽高、XY坐标、relate方法等布局方式,则此时返回的是 UIView。

    此时无法再进行UIButton的属性设置。

    -------------------------------------------------------------------------------------------------------------------------

    3、因此布局有几种写法:

    1、先布局,再通过block设置属性:需要手工block中的UIView修改具体的类型:UIButton,一般个人只有在层级布局时才使用。

    [[sagit addUIButton:@“name"]  x:0 y:0] block^(UIButton*view){
    
        [view text:@"xxxx"];
    }];

    2、用asXXX属性进行转换【属性少的时候,可以中间进行类型转换使用,太长了就不好用了。】

    [[[[sagit addUIButton:@“name"]  x:0 y:0] asUIButton] text:@"xxxx"];

    3、先设置属性、再布局【属性少的时候,这种写法最简短。】

    [[[sagit addUIButton:@“name"] text:@"xxxx"] x:0 y:0] 

    4、拆分两种写法:布局1行、属性1行。【属性多的时候,拆成两行是看起来最舒服的。】

    [[sagit addUIButton:nil] 20 height:20 x:0 y:0]
    [STLastUIButton text:@"hello"];

    通过STLastUIXXX类型,可以拿到刚添加的UI控件的具体类型。

    总结:

    代码的写法是多种的,具体喜欢哪种,都可以,只要知道两点:

    1、涉及到布局时返回的是基类:UIView。

    2、看第1点。

  • 相关阅读:
    ubuntu安装eclipse配置jdk环境
    ubuntu之使用sublime text3搭建Python IDE
    windows远程控制ubuntu尝试--未成功
    食物链(luogu 2024)
    玉米田Corn Fields(luogu 1879)
    互不侵犯(luogu 1896)
    又是毕业季I(luogu 1372)
    Supermarket
    Network of Schools [USACO5.3]
    最大乘积
  • 原文地址:https://www.cnblogs.com/cyq1162/p/13508009.html
Copyright © 2011-2022 走看看