<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一:添加導航控制器 上一篇博客完成了對底部的TabBar的設置,這一章我們完成自定義導航控制器(NYNavigationController)。 為啥要做自定義呢,因為為了更好地封裝代碼,并且系統的UINavigationController不能滿足我們的需求了,所以得自定義。 首先,我們在NYTabBarViewController的? - (void)addChildVc:(UIViewController?*)childVc title:(NSString?*)title image:(NSString?*)image selectedImage:(NSString?*)selectedImage方法中寫了這個: ~~~ // 先給外面傳進來的小控制器 包裝 一個導航控制器 NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc]; // 添加為子控制器 [self addChildViewController:nav]; ~~~ 來給設置的各個Controller包裝一個導航控制器。 這時候系統會自動給添加一個。然后呢我們要對導航控制器進行改進。 ### 框架結構 目前情況下的UI架構如下圖所示:一個IWTabBarController擁有4個導航控制器作為子控制器,每個導航控制器都有自己的根控制器(棧底控制器)? ![](https://box.kancloud.cn/2016-01-20_569f1d96d5886.jpg) * * * ### 重要代碼 1.給控制器包裝一個導航控制器并且放入tabbarController中 ~~~ // 先給外面傳進來的小控制器 包裝 一個導航控制器 NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc]; // 添加為子控制器 [self addChildViewController:nav]; ~~~ * * * ## 二:導航控制器左右item的設置 在NYMessageCenterViewController中我們添加了cell,并使之可以點擊,點擊后進入到另一個界面(test1) 再點擊界面的view進入另外一個界面(test2) 首先放入20行假數據——UITableView的數據源方法 返回一組,20行,每行內容cell設置 ~~~ #pragma mark - Table view data source 數據源方法 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return 20; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"ID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } cell.textLabel.text = [NSString stringWithFormat:@"test~~~~message - %d", indexPath.row]; return cell; } ~~~ 然后是cell的點擊方法了 不用死記全部方法名字,簡單敲一下tableview 查找didSele方法(學iOS對英語挺高老快了)靈活運用xcode的自動提示功能。 ~~~ #pragma mark - 代理方法 //cell的點擊事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NYTest1ViewController *test1 = [[NYTest1ViewController alloc] init]; test1.title = @"測試1控制器"; [test1.navigationController setNavigationBarHidden:NO]; [self.navigationController pushViewController:test1 animated:YES]; } ~~~ test1是我們自己做的一個測試類,其中我們做了兩個如圖:? ![](https://box.kancloud.cn/2016-01-20_569f1d96ee815.jpg) 這時候,我們的消息界面就有了cell的數據并且可以點擊了。如圖效果:? ![](https://box.kancloud.cn/2016-01-20_569f1d970cdd6.jpg)? (到test2的push和1的一樣,不過是用的view的touch方法) 這時候我們要做導航控制器的左右item了。? 然后我們設置導航控制器的左右item (寫私信按鈕等)? 如圖:? ![](https://box.kancloud.cn/2016-01-20_569f1d97344b7.jpg)? ![](https://box.kancloud.cn/2016-01-20_569f1d9746bda.jpg)? ![](https://box.kancloud.cn/2016-01-20_569f1d9756390.jpg) ~~~ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)]; //設置右側按鈕為不可點擊狀態 self.navigationItem.rightBarButtonItem.enabled = NO; NYLog(@"NYMessageCenterViewController-viewDidLoad"); } ~~~ 其中的UIBarButtonItem 的創建方法不是系統給的,而是我們為了實現黃色的效果自己寫的分類實現的。 ### 分類實現UIBarButtonItem的自定義創建方法: ~~~ // // UIBarButtonItem+Extension.m // 貓貓微博 // // Created by apple on 15-6-4. // Copyright (c) 2015年 znycat. All rights reserved. // #import "UIBarButtonItem+Extension.h" @implementation UIBarButtonItem (Extension) /** * 創建一個item * * @param target 點擊item后調用哪個對象的方法 * @param action 點擊item后調用target的哪個方法 * @param image 圖片 * @param highImage 高亮的圖片 * * @return 創建完的item */ +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage { UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; //設置圖片 [backBtn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; [backBtn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted]; [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; //設置尺寸 CGSize imageSize = backBtn.currentBackgroundImage.size; backBtn.frame = CGRectMake(0, 0, imageSize.width, imageSize.height); UIBarButtonItem *itemBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn]; return itemBtn; } @end ~~~ 這里設置尺寸用到了 ~~~ CGSize imageSize = backBtn.currentBackgroundImage.size; ~~~ 在我們學習UI的transform的時候,我們知道 是不能直接這么設置size的,但是為啥這里能呢? 很簡單 ,我們對UIView寫了一個分類 ~~~ // // UIView+Extension.m // 貓貓微博 // // Created by apple on 15-6-2. // Copyright (c) 2015年 znycat. All rights reserved. // #import "UIView+Extension.h" @implementation UIView (Extension) -(void)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } -(CGFloat)x { return self.frame.origin.x; } -(void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } -(CGFloat)y { return self.frame.origin.y; } -(void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } -(CGFloat)width { return self.frame.size.width; } -(void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } -(CGFloat)height { return self.frame.size.height; } -(void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; } -(CGSize)size { return self.frame.size; } -(void)setOrigin:(CGPoint)origin { CGRect frame = self.frame; frame.origin = origin; self.frame = frame; } -(CGPoint)origin { return self.frame.origin; } @end ~~~ ![](https://box.kancloud.cn/2016-01-20_569f1d97344b7.jpg)? ![](https://box.kancloud.cn/2016-01-20_569f1d9746bda.jpg)? 并且為了改變系統原生的 美麗的藍色情調,換成微博的黃色。。。? ![](https://box.kancloud.cn/2016-01-20_569f1d9777370.jpg) 我們要重寫NYNavigationController初始加載方法 (initialize)以及重寫pushViewController方法,讓push 的時候會自動帶著箭頭按鈕和右邊的更多按鈕(UIBarButtonItem) ![](https://box.kancloud.cn/2016-01-20_569f1d9756390.jpg) ~~~ // // NYNavigationController.m // 貓貓微博 // // Created by apple on 15-6-4. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYNavigationController.h" @interface NYNavigationController () @end @implementation NYNavigationController +(void)initialize { // 設置整個項目所有item的主題樣式 UIBarButtonItem *item = [UIBarButtonItem appearance]; // 普通狀態 NSMutableDictionary *textAttrsNormal = [NSMutableDictionary dictionary]; textAttrsNormal[NSForegroundColorAttributeName] = [UIColor orangeColor]; textAttrsNormal[NSFontAttributeName] = [UIFont systemFontOfSize:14]; [item setTitleTextAttributes:textAttrsNormal forState:UIControlStateNormal]; // 不可用狀態 NSMutableDictionary *textAttrsDisabled = [NSMutableDictionary dictionary]; textAttrsDisabled[NSFontAttributeName] = [UIFont systemFontOfSize:14]; textAttrsDisabled[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7]; [item setTitleTextAttributes:textAttrsDisabled forState:UIControlStateDisabled]; } /** * 重寫這個方法目的:能夠攔截所有push進來的控制器 * * @param viewController 即將push進來的控制器 */ -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { // 這時push進來的控制器viewController,不是第一個子控制器(不是根控制器) if (self.viewControllers.count > 0) { /* 自動顯示和隱藏tabbar */ viewController.hidesBottomBarWhenPushed = YES; // 設置左邊的箭頭按鈕 viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationbar_back" highImage:@"navigationbar_back_highlighted"]; // 設置右邊的更多按鈕 viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"]; } [super pushViewController:viewController animated:animated]; } -(void)back { #warning 這里要用self,不是self.navigationController // 因為self本來就是一個導航控制器,self.navigationController這里是nil的 [self popViewControllerAnimated:YES]; } -(void)more { //回到根 [self popToRootViewControllerAnimated:YES]; } @end ~~~ 最后就是各個頁面的調用了? 首頁:? ![](https://box.kancloud.cn/2016-01-20_569f1d97344b7.jpg) ~~~ - (void)viewDidLoad { [super viewDidLoad]; /* 設置導航欄上面的內容 */ self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"]; self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"]; } ~~~ 我:? ![](https://box.kancloud.cn/2016-01-20_569f1d9777370.jpg) ~~~ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"設置" style:0 target:self action:@selector(setting)]; } ~~~ 消息里面的寫私信(這里設置默認不可用狀態)? ![](https://box.kancloud.cn/2016-01-20_569f1d9746bda.jpg) ~~~ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)]; //設置右側按鈕為不可點擊狀態 self.navigationItem.rightBarButtonItem.enabled = NO; NYLog(@"NYMessageCenterViewController-viewDidLoad"); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看