2013-06-28 25 views
4

我有一個iPad應用,並在其中一個畫面我有一個UIToolbar設定爲viewControllernavigationItemtitleView。我也有一個left-和一個rightBarButtonItemUINavigationItem titleview的定位問題

當我進入橫向屏幕和旋轉設備時,titleView保持居中。但是,如果我做的相反(肖像進入和旋轉裝置),該titleView被右移。有沒有什麼辦法解決這一問題?這裏是我的代碼:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; 
UIToolbar *titleToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; 
titleToolbar.items = @[commentButton, spacer2, downloadButton, spacer3, homeButton, spacer4, pageDisplayButton, spacer5, searchButton]; 
titleToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
[titleView addSubview:titleToolbar]; 
self.navigationItem.titleView = titleView; 

編輯:

self.navigationItem.titleView.frame.size是在這兩種情況下是相同的,即改變的事情是origin.x

+0

嘗試使用'sizeToFit'。 – Bhavin

+0

改變你的導航控制器的位置,當你腐爛你的設備:) – 2013-07-03 10:23:03

回答

3

如果你正在使用的工具欄,不添加titleView作爲子視圖。相反,添加項目數組。

UIToolbar *titleToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; 

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, titleToolbar.frame.size.height)]; 

將欄按鈕項目添加爲titleViewBtn並使邊框平滑。

titleToolbar.items = @[commentButton, spacer2, downloadButton, spacer3, homeButton, spacer4, **titleViewBtn** , pageDisplayButton, spacer5, searchButton]; 
titleToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;