2014-12-16 3 views
1

есть способ реализовать приведенный ниже код для стилизации моей навигационной панели в ViewDidLoad или глобально каким-то образом вместо вставки в каждый VC? Благодарю.Навигация Стилирование по всему окну

// Navagation styling 
    viewController.title = @"NAV TITLE"; 
    [viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]]; 

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil 
                     style:UIBarButtonItemStylePlain 
                     target:viewController 
                     action:nil]; 

    rightBarButton.image = [UIImage imageNamed:@"settings_cog"]; 
    rightBarButton.tintColor = [UIColor grayColor]; 

    viewController.navigationItem.rightBarButtonItem = rightBarButton; 
+0

Создайте подкласс UIViewController, вставьте над ним код и распространите его на все остальные контроллеры. – Kampai

+0

Лучше создать категорию и добавить - (void) addRightButtonItem; метод к нему – Abhishek

ответ

2

Вы можете создать BaseViewController и поставить выше код в нем и расширить все контроллеры от BaseViewController.

Делай так:

Создать Objective-C класс BaseViewController.h и .m

В BaseViewController.h

@interface BaseViewController : UIViewController 
{ 
    NSString *_title; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle;  

@end 

В BaseViewController.m

@implementation BaseViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle 
{ 
    _title = navTitle; 
} 

- (void)viewDidLoad 
{ 
    self.title = _title; 
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]]; 
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil 
                    style:UIBarButtonItemStylePlain 
                    target:viewController 
                    action:nil]; 
    rightBarButton.image = [UIImage imageNamed:@"settings_cog"]; 
    rightBarButton.tintColor = [UIColor grayColor]; 
    self.navigationItem.rightBarButtonItem = rightBarButton; 
} 

@end 

Теперь вы можете создать свой контроллер представления и расширить его из BaseViewController, если UIViewController.

Дайте мне знать, если возникнут какие-либо вопросы.

+0

вы можете объяснить, как это делается, пожалуйста? – Luke

+0

@ Luke обновил сообщение. Взгляни. –

+0

Великий это вместе с http://stackoverflow.com/questions/6249416/adding-more-than-two-button-on-the-navigationbar помог мне добавить элемент в правые навигационные элементы, основанные на успехе или неудаче условия с минимальными кодами , –

Смежные вопросы