【问题标题】:Customizing Navigation Controller for all ViewControllers为所有 ViewController 自定义导航控制器
【发布时间】:2011-11-29 14:34:00
【问题描述】:

我的 AppDelegate.h

//
//  AppDelegate.h
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    UITabBarController *tabBarController;
}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

我的 AppDelegate.m

//
//  AppDelegate.m
//
#import "AppDelegate.h"

#import "AppNavigationController.h"

#import "ExamViewController.h"
#import "SignsViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_tabBarController release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    // Initializating our Tab Bar Controller
    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    // Exam Controller Setup
    ExamViewController *examViewController = [[ExamViewController alloc] initWithStyle:UITableViewStylePlain];
    AppNavigationController *examNavigationController = [[AppNavigationController alloc] initWithRootViewController:examViewController];

    examViewController.title = @"Экзамен";
    examViewController.tabBarItem.image = [UIImage imageNamed:@"icon_exam.png"];
    // -------------------------------------

    // Signs Controller Setup
    SignsViewController *signsViewController = [[SignsViewController alloc] initWithStyle:UITableViewStylePlain];
    AppNavigationController *signsNavigationController = [[AppNavigationController alloc] initWithRootViewController:signsViewController];

    signsViewController.title = @"Знаки";
    signsViewController.tabBarItem.image = [UIImage imageNamed:@"icon_signs.png"];

    // -------------------------------------

    [tabBarController  setViewControllers:[NSArray arrayWithObjects:examNavigationController, signsNavigationController, nil]];

    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    [examNavigationController release];
    [examViewController release];

    [signsNavigationController release];
    [signsViewController release];

    return YES;
}

我还有空的 UINavigationController。

我想要实现的是为所有使用它的视图控制器定制导航栏 例如:现在我只有两个视图控制器,现在我将 self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; 放在 ExamViewController.m 和 SignsViewController.m 的 viewDidLoad 方法中 但可能我想添加两个或更多选项卡并想在一个地方自定义导航栏。 那么问题来了:如何在一个地方自定义导航栏,使其在每个视图控制器中看起来都一样,而不在 viewDidLoad 方法中在每个视图控制器中自定义它?

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationbar


    【解决方案1】:

    试试这个:。

    1. 创建一个新的视图控制器,比如说 ViewControllerTemplate.h
    2. 将设计设置为 .m 文件中的 ViewControllerTemplate 导航控制器
    3. 然后每次你想创建一个视图控制器时,只需将 ViewControllerTemplate.h 设置为它的超类,它就会继承设计。

      ViewControllerTemplate *newController = [[ViewControllerTemplate alloc]init];
      

    【讨论】:

    • 我有文件夹 Templates,我的 AppNavigationController 放在那里,问题是如何在其中自定义视图。
    • 谢谢,我已经把 self.navigationBar.barStyle = UIBarStyleBlackOpaque;在 AppNavigationController viewDidLoad 方法中。我试过把 self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;之前但没有结果。
    【解决方案2】:

    如果您使用的是 iOS 5,那么只有一种新方法是 UIAppearence。见这里:Custom Appearance for UIKit Controls

    Steve Baranski 编写的另一个很棒的教程如何在 iOS 5 中自定义 UI: User Interface Customization in iOS 5

    【讨论】:

    • UIAppearance 仅适用于 iOS 5。如果您需要其他 iOS 版本的解决方案,shannogas 回答是您可以做到的一种方法。
    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多