将新视图Controller推入堆栈时,UINavigationController自定义LabelView

时间:2013-06-26 15:06:45

标签: ios objective-c cocoa-touch ios6 uinavigationcontroller

我的初始视图控制器设置了UINavigationController,允许用户输入值。它还有一个自定义UILabel作为titleView,这样我就可以显示一个带有绿色文本的完全“扁平”导航栏。

然后,当他们按下UIButton时,它会将新视图推送到堆栈,这显然会消除带有我的自定义titleView的UINavigationItem

我的问题是:如何继续可靠地使用导航栏titleView的自定义视图?

我显然尝试过使用“delegate”方法(willPushdidPush),但第一种方法显然还没有topItem,所以它最终将我的UILabel添加到当前navigationItemdidPush视图控制器动画显示与正在应用的新titleView标签之间存在明显的延迟。

我已经尝试过google了很多,但似乎没有什么工作正常,我不想在推新视图控制器时丢失动画。

有什么想法吗?

编辑:在下面发布了我的代码的粗略轮廓示例,它不是100%完美,因为它是我正在做的概述,而不是直接从XCode粘贴。

// This code lives in the app delegate

// Set up the root view controller that needs to be contained
UIViewController *myFrontView = [[MYFrontViewController alloc] init];

// Set up the uinavigationcontroller
UINavigationController *myNavController = [[MYNavigationController alloc] initWithRootViewController: myFrontView];

// Customise the initial UINavbar titleView:
UILabel *myLabel = [[UILabel alloc] initWithText:@"My custom text"];

// Change the text Color.
myLabel.textColor = [UIColor redColor];

// Set my new label as the custom titleview.
[myNavController.navigationBar.topItem setTitleView: myLabel];

// Boilerplate code to make myNavController the rootViewController of the window etc.

1 个答案:

答案 0 :(得分:0)

这是因为您要设置titleView的{​​{1}}。这只会暂时更改当前的标题视图。

要正确更改标题视图,请使用UINavigationController的{​​{1}}属性。导航控制器框架将根据您当前的导航项目设置titleView的视图,并确保动画流畅。

相关问题