iOS8 自定義navigationItem.titleView


navigationBar其實有三個子視圖,leftBarButtonItem,rightBarButtonItem,以及titleView。前兩種的自定義請參考http://www.cnblogs.com/6duxz/p/4030916.html

titleView的自定義就更簡單了,把它看成一個視圖,替換或者添加子視圖都可以。以下我在titleView添加了兩個button

UIButton* actionNewButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    [actionNewButton setTitle:@"測試1" forState:UIControlStateNormal];
    actionNewButton.tag=0;
    [actionNewButton setTitleColor:[UIColor colorWithRed:0.1 green:0.63 blue:0.96 alpha:0.93] forState:UIControlStateNormal];
    //actionNewButton.backgroundColor=[UIColor grayColor];
    
    UIButton* classicAction = [[UIButton alloc]initWithFrame:CGRectMake(50, 0, 50, 40)];
    [classicAction setTitle:@"測試2" forState:UIControlStateNormal];
    classicAction.tag=1;
    [classicAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //classicAction.backgroundColor=[UIColor blueColor];
    
    UIView* titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    //titleView.backgroundColor=[UIColor redColor];
    [titleView addSubview:actionNewButton];
    [titleView addSubview:classicAction];
    
    self.navigationItem.titleView=titleView;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM