导航栏颜色和标题
我有很多 .xib 文件,我尝试以编程方式重新创建它们。
唯一的问题是,我无法再自定义导航栏。
De versions 函数由具有多个栏按钮项的另一个视图调用,因此该代码对于其他函数是通用的。
- (void)versions
{
VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
[verController setDelegate:self];
[self loadPopupView:verController];
[verController release];
}
- (void)loadPopupView:(UIViewController *)viewController
{
if (popOverController != nil && [popOverController isPopoverVisible])
{
[popOverController dismissPopoverAnimated:YES];
}
if(![popOverController isPopoverVisible] || ![popOverController.contentViewController isKindOfClass:[viewController class]])
{
popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
popOverController.popoverContentSize = CGSizeMake(320, 500);
UIBarButtonItem *buttonLocation;
if([viewController isKindOfClass:[CommentaryViewController class]])
buttonLocation = commentaryButton;
else if([viewController isKindOfClass:[PropertiesViewController class]])
buttonLocation = propertiesButton;
else
buttonLocation = versionsButton;
[popOverController presentPopoverFromBarButtonItem:buttonLocation permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
VersionsViewController.m
- (id)initWithDocument:(Document *)doc
{
self = [super init];
if(self)
{
self.document = doc;
self.title = @"other title"; //does not work either
//I just tried everything I could think of :P
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
self.navigationItem.titleView.backgroundColor = [UIColor redColor];
self.navigationController.navigationItem.titleView.backgroundColor = [UIColor greenColor];
self.navigationController.tabBarController.tabBar.backgroundColor = [UIColor blueColor];
self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
}
return self;
}
有人能看到我做错了什么吗?
编辑:
来自 self.title 和 self.navigationController.title 的 NSLOG 都是“null”
当我创建 navigationController 添加视图并将 navigationController 添加到弹出窗口时,我得到 2 个栏,然后我可以设置 navigationController 的标题,但仍然不行颜色。
I had a lot of .xib files and I try to recreate them programmatically.
The only problem is, is that I can't customize my navigationbar any more.
De versions function is called by another view with multiple bar button items, so the code is generic to other functions to.
- (void)versions
{
VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
[verController setDelegate:self];
[self loadPopupView:verController];
[verController release];
}
- (void)loadPopupView:(UIViewController *)viewController
{
if (popOverController != nil && [popOverController isPopoverVisible])
{
[popOverController dismissPopoverAnimated:YES];
}
if(![popOverController isPopoverVisible] || ![popOverController.contentViewController isKindOfClass:[viewController class]])
{
popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
popOverController.popoverContentSize = CGSizeMake(320, 500);
UIBarButtonItem *buttonLocation;
if([viewController isKindOfClass:[CommentaryViewController class]])
buttonLocation = commentaryButton;
else if([viewController isKindOfClass:[PropertiesViewController class]])
buttonLocation = propertiesButton;
else
buttonLocation = versionsButton;
[popOverController presentPopoverFromBarButtonItem:buttonLocation permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
VersionsViewController.m
- (id)initWithDocument:(Document *)doc
{
self = [super init];
if(self)
{
self.document = doc;
self.title = @"other title"; //does not work either
//I just tried everything I could think of :P
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
self.navigationItem.titleView.backgroundColor = [UIColor redColor];
self.navigationController.navigationItem.titleView.backgroundColor = [UIColor greenColor];
self.navigationController.tabBarController.tabBar.backgroundColor = [UIColor blueColor];
self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
}
return self;
}
Can someone see what I did wrong?
EDIT:
NSLOG from self.title and self.navigationController.title are both 'null'
When I create a navigationController add the view and add the navigationController to the popup i get 2 bars and then I can set the title of the navigationController but still not the color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是好奇:如果您有工作 nib 文件,为什么要以编程方式重新创建它们?
在
initWithDocument:
中,您尚未分配nvaigationController
。您需要手动构建其中一个,并在其中安装VersionViewController
(navigationController
将在您完成此操作后设置)。自动完成所有这些工作是我们使用 nib 文件的原因之一。编辑任何时候“没有发生任何事情”,请检查您正在消息传递的对象是否为
nil
。我确信当您到达时navigationController
仍然是nil
。您还应该NSLog
您关心的对象并查看它们的地址。您可能不小心创建了多个视图或多个导航控制器。当您尝试手动构建这些东西时,这是很常见的做法。在大多数情况下,笔尖是正确的解决方案。Just curious: if you have working nib files, why are your recreating them programmatically?
In
initWithDocument:
, you haven't assigned anvaigationController
yet. You'll need to build one of those by hand, and installVersionViewController
in it (navigationController
will be set after you've done that). Having all of this work automatically is one of the reasons we use nib files.EDIT Anytime "nothing happens" check that the object you're messaging is not
nil
. I'm certainnavigationController
is stillnil
when you get to it. You should alsoNSLog
the objects you care about and look at their addresses. You probably are accidentally creating multiple views or multiple navigation controllers. This is very common to do when you try to build this stuff by hand. Nibs are the correct solution in most cases.