我在 Storyboard中添加了一个 UINavigationBar
。我的应用程序必须动态更改栏的标题。首先,我尝试更改 Storyboard 中的标题并运行该应用程序,但标题没有出现。然后我尝试通过代码添加标题。首先,我将 UINavigationItem
中的 IBOutlet
连接到我的 @interface
并尝试添加如下标题:
_navibarTitle.title = @"Something";
同样没有成功,标题没有出现。由于我的应用程序必须隐藏 UINavigationController
的默认栏并使用自定义栏,我什至试过这个:
self.navigationItem.title = @"Something";
还有这个:
self.navigationController.navigationItem.title = @"HI";
他们也没有成功。 (最后两个我知道它们不会工作,因为默认的导航栏被隐藏了)。所以我想我没主意了。有什么问题吗?
最佳答案
为屏幕或导航栏设置标题的简单且最好的方法是:
self.title = @"你的标题"
它会自动在导航栏上显示您的标题。您可以从this获得帮助问题也是如此。
同样的其他方法:
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textAlignment = NSTextAlignmentCenter;
navLabel.text = @"Your Title";
self.navigationItem.titleView = navLabel;
我希望这会奏效。
关于ios - 自定义导航栏不更改/显示标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24605834/