您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發實戰(四):為導航欄加上返回與更多按鈕

編輯:iOS 開發綜合

一、系統方法不夠用

系統提供的便利建構子長這樣:

viewController.navigationItem.leftBarButtonItem =
    [[UIBarButtonItem alloc] initWithImage:image
                                     style:style
                                    target:target
                                    action:action];

問題是它只吃一張圖。而設計稿通常會要求「正常態」與「按下時」兩種視覺,於是我們替 UIBarButtonItem 寫一個分類,擴充出能同時指定兩張圖的方法。

@implementation UIBarButtonItem (Extension)

+ (UIBarButtonItem *)itemWithImageName:(NSString *)ImageName
                        highImageName:(NSString *)highImageName
                               target:(id)target
                               action:(SEL)action {
    UIButton *btn = [[UIButton alloc] init];

    [btn setBackgroundImage:[UIImage imageWithName:ImageName]
                   forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithName:highImageName]
                   forState:UIControlStateHighlighted];

    // 讓按鈕尺寸剛好等於圖片,用 UIImage 的分類取 currentBackgroundImage
    btn.size = btn.currentBackgroundImage.size;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

@end

這裡的關鍵是 initWithCustomView:。UIBarButtonItem 允許塞入任何 UIView,因此用一個 UIButton 包住,就能自由掌控兩種狀態的圖片、尺寸與點擊區域。

二、需求:讓所有子頁面共用同一組導航按鈕

專案裡有很多層級的子頁面,每一頁的左上角都要能返回上一頁,右上角則是回到首頁。直覺的做法是每頁各寫一次,但如果有十頁、一百頁,就是把同樣的程式碼複製一百次——這顯然不合理。

第二個想法是抽一個共同父類。這在 iOS 裡都行不通:一旦繼承了自己的基礎控制器,就不能再繼承 UITableViewController;而且現實專案中控制器種類繁多,用繼承去約束所有人會讓架構僵化。iOS 的控制器設計本就鼓勵直接繼承系統類別,不該硬加一層繼承關係。

第三個方案才是正解:攔截 push 操作。既然所有跳轉都會經過自定義的 UINavigationController,那就覆寫它的 pushViewController:animated:,在頁面被推入堆疊的那一刻統一設定按鈕。

三、攔截 push

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 只要不是棧底的第一頁,就統一加上一對按鈕
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;

        viewController.navigationItem.leftBarButtonItem =
            [UIBarButtonItem itemWithImageName:@"navigationbar_back"
                                highImageName:@"navigationbar_back_highlighted"
                                       target:self action:@selector(back)];

        viewController.navigationItem.rightBarButtonItem =
            [UIBarButtonItem itemWithImageName:@"navigationbar_more"
                                highImageName:@"navigationbar_more_highlighted"
                                       target:self action:@selector(more)];
    }
    [super pushViewController:viewController animated:YES];
}

- (void)back {
    [self popViewControllerAnimated:YES]; // self 就是目前使用的導航控制器
}

- (void)more {
    [self popToRootViewControllerAnimated:YES];
}

這個做法的威力在於「攔截」。所有 push 都會經過這裡,因此不必拜託每個頁面配合。若某頁確實需要不同的右上角按鈕,只要在自己的 viewDidLoad 中重新指定即可覆蓋。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"真的可以設置"
                                         style:UIBarButtonItemStyleDone
                                        target:self action:nil];
}

四、順手做的兩個分類

為了讓圖片載入能自動適配 iOS 7 的 _os7 後綴素材,替 UIImage 加一個分類:

+ (UIImage *)imageWithName:(NSString *)name {
    UIImage *image = nil;
    if (iOS7) {
        NSString *newName = [name stringByAppendingString:@"_os7"];
        image = [UIImage imageNamed:newName];
    }
    if (image == nil) {
        image = [UIImage imageNamed:name];
    }
    return image;
}

再替 UIView 加上 x、y、width、height、size 這組便捷屬性,內部一律改寫 frame。之後寫版面時就能用 btn.centerX = self.view.width * 0.5 這種直覺寫法,不必每次都拆 frame 再組回去。

五、首頁與子頁面的串接

// 首頁:設定自己的左右按鈕
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem =
        [UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch"
                            highImageName:@"navigationbar_friendsearch_highlighted"
                                   target:self action:@selector(friendsearch)];
    self.navigationItem.rightBarButtonItem =
        [UIBarButtonItem itemWithImageName:@"navigationbar_pop"
                            highImageName:@"navigationbar_pop_highlighted"
                                   target:self action:@selector(pop)];
}

- (void)friendsearch {
    YYOneViewController *one = [[YYOneViewController alloc] init];
    one.title = @"One";
    [self.navigationController pushViewController:one animated:YES];
}

// 子頁面:單純往下推
- (IBAction)jump2Two {
    YYTwoViewController *two = [[YYTwoViewController alloc] init];
    two.title = @"Two";
    [self.navigationController pushViewController:two animated:YES];
}

最後記得把這幾個分類的 header 全部放進 Prefix.pch,並在 DEBUG 模式下才打開 YYLog、正式版關閉,避免除錯訊息外洩到 production 環境。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved