標籤:
主題設定,初始化的代碼,通過執行這些代碼來設定對應的navigation和barbutton的屬性
/** * 只執行一次的代碼 */+(void)initialize{ //設定nav對應的屬性. [self setNavigationBarTheme]; //設定barbutton對應的屬性. [self setBarButtonTheme ];}
對應屬性的設定
+(void)setBarButtonTheme{ // UIBarButtonItem *appearence = [UIBarButtonItem appearance]; NSMutableDictionary *textAttrs =[[NSMutableDictionary alloc]init]; textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor]; textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15]; [appearence setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; NSMutableDictionary *highTextAttrs =[[NSMutableDictionary alloc]init]; highTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor]; highTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15]; [appearence setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted]; NSMutableDictionary *disableTextAttrs =[[NSMutableDictionary alloc]init]; disableTextAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15]; [appearence setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled]; //為了讓按鈕的背景消失,可以設定一張完全透明的背景圖片 [appearence setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];}+(void)setNavigationBarTheme{ UINavigationBar *appearence = [UINavigationBar appearance]; NSMutableDictionary *textAttrs= [NSMutableDictionary dictionary]; textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20]; textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor]; [appearence setTitleTextAttributes:textAttrs];}
定義為類方法,容易調用。
最重要的方法還是重載的push方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.viewControllers.count>0) { //如果push進來的不是棧底控制器的話 viewController.hidesBottomBarWhenPushed = YES; viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" highImage:@"navigationbar_back_highlighted" target:self action:@selector(back)]; viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" highImage:@"navigationbar_more_highlighted" target:self action:@selector(more)]; } [super pushViewController:viewController animated:YES];}
push中的來設定對應的屬性,包括對應的背景和高亮背景點擊時間對應的響應函數。
設定對應的按鈕點擊的響應內容。
-(void)more
{
[self popToRootViewControllerAnimated:YES];
}
-(void)back
{
[self popViewControllerAnimated:YES];
}
代碼搭建記事本架構(二)