於titleview框會自動調整,如果你想獲得這樣的結果,你可以試試這個:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame];
[titleLabel setTextAlignment:UITextAlignmentRight];
[titleLabel setText:@"Your title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
self.navigationItem.titleView = titleLabel;
注意:一定不要設置你的視圖控制器self.title否則你會覆蓋titleView
另一種解決方案可以把titleLabel放入一個UIBarButtonItem(customView)並設置它喜歡self.navigationItem.rightBarButtonItem這樣你就不需要設置文字對齊了,標籤的框架可以是正確的文字大小
然後添加爲子視圖。 – Desdenova 2013-04-30 09:50:45