[BS] 小知識點總結

來源:互聯網
上載者:User

標籤:

1.

UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]]; //initWithImage方法的好處是建立的imageView和圖片的尺寸一樣

2.

//看UI層級,tabBar上的按鈕的確是UITabBarButton類型,但是這個類是私人的不讓用,故不能調[view isKindOfClass:[UITabBarButton class]

        if (![view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

            continue;//如果不是這個類,直接跳過後面的代碼,繼續判斷下一個

        }

3.

執行個體方法: [obj class]          類方法: [Class class];

WZLog(@"執行個體方法:%@,類方法:%@",[self class],[UITabBarItem class]);

4.

   //使用代碼設定圖片的渲染模式

     UIImage *image = [UIImage imageNamed:@"tabBar_essence_click_icon"];

     UIImage *renderImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//原始照片不渲染

     vc01.tabBarItem.selectedImage = renderImage;

5.

//更換TabBar底部條 @property(nonatomic,readonly) UITabBar *tabBar

    WZTabBar *tabBar = [[WZTabBar alloc]init];

    //self.tabBar = tabBar; 報錯,readonly不能賦值,採用KVC賦值

    [self setValue:tabBar forKey:@"tabBar"];

6.

+ (void)initialize { //涉及appearance的設定要寫在initialize中,此方法只在該類第一次被使用時才調用。

  UITabBarItem *item = [UITabBarItem appearance];

    [item setTitleTextAttributes:attr forState:UIControlStateNormal];

    [item setTitleTextAttributes:selectAttr forState:UIControlStateSelected];//注意此處為UIControlStateSelected,不能寫成Highlighted了

}

    //原理是:setTitleTextAttributes:forState: 函數後面有備忘UI_APPEARANCE_SELECTOR,可以通過 [Class appearance] setTitleTextAttributes: forState: ]; 來同一設定字型顏色。

7.

UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];

    [btnLeft setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];

    [btnLeft setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:UIControlStateHighlighted];

    //注意必須設定btnLeft的frame或者size

    btnLeft.size = btnLeft.currentBackgroundImage.size;

//自訂導覽按鈕時,事件應傳給btnLeft;如果使用initWithTitle:target:action:方法初始化,可以把事件傳給BarButtonItem

    [btnLeft addTarget:self action:@selector(tagSubClick) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnLeft];

8.

//設定發布按鈕的frame

    CGFloat btnW = btnPublish.currentBackgroundImage.size.width;

    CGFloat btnH = btnPublish.currentBackgroundImage.size.height; //currentBackgroundImage屬於UIImage:NSObject,不屬於UIView,重寫UIView(Frame)對UIImage不管用,UIImage自己就有.size屬性。不能換成btnPublish.currentBackgroundImage.height;

9.

//從xib或sb載入的控制項時,會調用此方法,純程式碼定義繼承UIView類初始化時不會調

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    NSLog(@"%s",__FUNCTION__);

    if (self = [super initWithCoder:aDecoder]) {

    }

    return self;

}

//純程式碼定義UI控制項類的實際初始化方法

- (instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {

    }

    return self;

}

10.

//initWithFrame中設定frame,會被如下方法重新布局

- (void)layoutSubviews{

[super layoutSubviews];

}

//viewDidLoad完了調viewWillAppear,然後在viewDidLayout,最後viewDidAppear

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];

}

11.

(1. UIView(所有控制項均繼承它)有個contentMood屬性,用來設定控制項裡面填充的文字/圖片等的對齊(如:

預設UIViewContentModeScaleToFill,UIViewContentModeTop/Left/Right/TopLeft等),但一般只對UIImageView圖片框有效,對Button等基本無效。

(2. 但是所有繼承自UIControl的控制項都有contentHorizontalAlignment和contentVerticalAlignment這2個屬性,可以通過如下代碼設定UIControl對象內容的水平和垂直對齊:

  btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//預設center

      btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//預設center

12.

在Storyboard或XIB中設定UILabel,UITextField,UITextView等文字時,如果想換行,按Opt +Enter或者Ctrl+Enter都可以。如果是在代碼中設定的話,在需要換行的位置加入\n即可。如:@“adjh\n55655”

 

[BS] 小知識點總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.