Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 定制UINavigationBar的最惯用的iOS5方法?_Iphone_Ios_Cocoa Touch_Ios5 - Fatal编程技术网

Iphone 定制UINavigationBar的最惯用的iOS5方法?

Iphone 定制UINavigationBar的最惯用的iOS5方法?,iphone,ios,cocoa-touch,ios5,Iphone,Ios,Cocoa Touch,Ios5,我正在自定义应用程序中导航控制器的外观,如下所示: 正如我在几个小时的研究后发现的那样,有很多不同的方法可以做到这一点,有些方法非常老练,有些则不那么老练。我感兴趣的是找到一种苹果最受欢迎/最优雅的方式来实现这一点,随着应用程序的发展,这种方式将带来最少的痛苦。到目前为止,我已经研究了一些方法: 1)我通过[UINavigationBar外观]应用图像更改了导航栏的背景/高度,看起来效果不错 UIImage *navBarImage = [UIImage imageNamed:@"naviga

我正在自定义应用程序中导航控制器的外观,如下所示:

正如我在几个小时的研究后发现的那样,有很多不同的方法可以做到这一点,有些方法非常老练,有些则不那么老练。我感兴趣的是找到一种苹果最受欢迎/最优雅的方式来实现这一点,随着应用程序的发展,这种方式将带来最少的痛苦。到目前为止,我已经研究了一些方法:

1)我通过[UINavigationBar外观]应用图像更改了导航栏的背景/高度,看起来效果不错

UIImage *navBarImage = [UIImage imageNamed:@"navigation-bar.png"];

[[UINavigationBar appearance] setBackgroundImage:navBarImage
                                   forBarMetrics:UIBarMetricsDefault];
这似乎是实现背景/高度变化的最“现代”的方式,尽管它很可能无法经受住方向的变化。这里有什么可以改进的吗

2)我在按下视图的viewDidLoad中将默认的后退按钮替换为以下内容

// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];

//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];

//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

// offset the back button
button.contentEdgeInsets = UIEdgeInsetsMake(10, 5, -10, -5);

//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
我对这个解决方案不太满意,因为它将工具栏的定制留给导航堆栈顶部的控制器。看起来他们更希望您将UINavigationBar完全子类化,并在导航控制器中一次性替换它:

还可以通过使用initWithNavigationBarClass:toolbarClass:方法初始化导航控制器来指定自定义UINavigationBar子类

那是建议的路线吗?我无法通过[UIBarButtonim外观]替换UINavigationBar的默认后退按钮,因为它仍然试图在按钮中显示文本,当您删除文本时,按钮根本不显示。建议

3)页面标题应可通过navigationItem.titleView替换为其他视图。还有更好的吗

谢谢

1)您应该为两个UIBarMetrics设置两个图像(UIBarMetricsDefault和一个单独的UIBarMetricsLandscapePhone图像)。因此

2) 您可以创建UINavigationBar子类(正如您也提到的,这将是默认的Apple方式)。或者,如果它只是一个按钮,也许你可以通过传入“”(空文本)来破解它的行为

3) 不知道你的意思。您可以设置导航栏的标题,它将显示您想要的任何标题。或者您应该能够为titleView插入另一个视图

请注意,setBackgroundImage是iOS 5.0及更高版本

UIImage *navBarImage = [UIImage imageNamed:@"navigation-bar.png"];
UIImage *navBarImage_Landscape = [UIImage imageNamed:@"navigation-bar-landscape.png"];

[[UINavigationBar appearance] setBackgroundImage:navBarImage
                                   forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:navBarImage_Landscape
                               forBarMetrics:UIBarMetricsLandscapePhone];