iOS開發 封裝導航欄

  多數情況下,開發項目期間需要用到導航欄,在項目開發搭建架構時便可通過重寫

NavigationController的父類方法

對導航欄提前封裝好,比如按鈕,文字顏色,背景圖片和顏色


+ (void)initialize
{
    UINavigationBar *navgationBar = [UINavigationBarappearance];
    [navgationBar setShadowImage:[UIImagenew]];//設置陰影圖片
    navgationBar.tintColor = [UIColorwhiteColor];//設置導航條顏色
    CGFloat width = [UIScreenmainScreen].applicationFrame.size.width;
    [navgationBar setBackgroundImage:[UIImageimageFromColor:[UIColornavigationTintColor]size:CGSizeMake(width,64)]forBarMetrics:UIBarMetricsDefault];//設置背景圖片和顏色
    [navgationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColorbarButtonItemTintColor]}];//設置文字顏色
}

- (void)viewDidLoad
{
    [superviewDidLoad];
    self.delegate =self;//設置自身的代理方法
    self.interactivePopGestureRecognizer.delegate = self;
}



- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated//重寫push的代理方法
{
    if ([self.viewControllerscount] > 0) {
        viewController.hidesBottomBarWhenPushed =YES;
    }
    [superpushViewController:viewControlleranimated:animated];
    if ([self.viewControllerscount] > 1 && !viewController.navigationItem.leftBarButtonItem) {//判斷是否會根視圖
        UIButton *backButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImageimageNamed:@"nav_back"]forState:UIControlStateNormal];//設置返回按鈕圖片
        backButton.bounds =CGRectMake(0,0,12,22);
        [backButton addTarget:selfaction:@selector(backToPageAction)forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:backButton];
    }
}

- (void)backToPageAction//設置返回按鈕
{
    [selfpopViewControllerAnimated:YES];
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章