2013-04-30 33 views
2

我需要將navigationItem TitleView對齊到最右側。 (其中navigationItem rightBarButtonItem通常出現)right align self.navigationItem setTitleView

這怎麼辦?

我都試過,但沒有運氣仍然放在titleview的在中心:

CGRect titleViewPos = CGRectMake(300, 0, 200, 10); 
self.navigationItem.titleView.frame = titleViewPos; 
+1

然後添加爲子視圖。 – Desdenova 2013-04-30 09:50:45

回答

4
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
lbl.textAlignment = UITextAlignmentRight; 
lbl.backgroundColor = [UIColor clearColor]; 
lbl.text = @"TITLEVIEW"; 
self.navigationItem.titleView = lbl; 

你會得到輸出

enter image description here

0

只是一個想法:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame = CGRectMake(300, 0, 200, 10); 
[btn setTitle:@"titleView" forState:UIControlStateNormal]; 
self.navigationItem.titleView = btn; 
0

您需要將UIElement(UIView,UILabel)傳遞給導航項目,然後您可以設置位置並使用導航標題或左側按鈕或右側按鈕進行其他自定義。 願這能幫助你。

0

試試這個,可能是使用全

UILabel *lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)]; 
lbNavTitle.textAlignment = NSTextAlignmentRight; 
lbNavTitle.backgroundColor = [UIColor clearColor]; 
lbNavTitle.text = @"hello World"; 
self.navigationItem.titleView = lbNavTitle; 
0

於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這樣你就不需要設置文字對齊了,標籤的框架可以是正確的文字大小

0

這一切都不適合我。我接受了Desdanova的建議,並將其添加爲子視圖。有道理,因爲就是這樣!用你想要的任何幀位置初始化它,然後

[self.view addSubView:lbl]; 

和Voila!