2010-05-09 35 views
1

我將一些按鈕添加到已有的NavigationController中。這兩個按鈕被添加到一個UIView,它被推到NavigationItem上。按鈕停止並重新加載UIWebView。我該如何解決Objective-C中的NavigationController和UIToolbar偏移問題?

alt text http://i42.tinypic.com/jr2sd5.jpg

問題是,有這麼正在看都看很醜陋輕微的偏移問題。我希望我可以將UIToolbar設置爲透明或清晰的背景,但這似乎不是一種選擇。似乎也無法使用負偏移。我已經有了顏色匹配,但是如果仔細觀察,會有1px或2px的高亮顯示,導致視覺不匹配,然後在底部產生輕微偏移。

下面的一些相關代碼(based on this,入站Google員工)。我有什麼選擇來解決這個問題?

// create a toolbar for the buttons 
UIToolbar* toolbar = [[UIToolbar alloc] 
     initWithFrame:CGRectMake(0, 0, 100, 45)]; 
[toolbar setBarStyle: UIBarStyleDefault]; 
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0]; 
toolbar.tintColor = colorForBar; 
    [colorForBar release]; 

//[toolbar setTranslucent:YES]; 

// create an array for the buttons 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

// create a standard reload button 
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
      target:self 
      action:@selector(reload)]; 
reloadButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:reloadButton]; 
[reloadButton release]; 

// create a spacer between the buttons 
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
      target:nil 
      action:nil]; 
[buttons addObject:spacer]; 
[spacer release]; 

// create a standard delete button with the trash icon 
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
      target:self 
      action:@selector(stopLoading)]; 
stopButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:stopButton]; 
[stopButton release]; 

// put the buttons in the toolbar and release them 
[toolbar setItems:buttons animated:NO]; 
[buttons release]; 

// place the toolbar into the navigation bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
      initWithCustomView:toolbar]; 
[toolbar release]; 

更新:對齊通過以下@Jacob固定由建議。現在,如何解決突出顯示不匹配問題?

alt text http://i40.tinypic.com/21j7fwy.jpg

回答

4

我以前有同樣的確切問題,以及一些黑客周圍後,我想通了這一點:

您需要將UIToolbar的的height設置frame44.01這個上班。

編輯:回覆:突出不匹配 - >你可以嘗試設置backgroundColorcolorForBar

UIToolbar* toolbar = [[UIToolbar alloc] 
          initWithFrame:CGRectMake(0, 0, 100, 44.01)]; 
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0]; 
toolbar.tintColor = colorForBar; 
toolbar.backgroundColor = colorForBar; 
+1

極好的解決了對齊問題。有關如何使斜角/突出顯示匹配的建議? – buley 2010-05-09 19:21:31

+0

@editor,什麼突出顯示不匹配? – 2010-05-09 19:25:45

+0

查看上面更新的圖像。在頂部有一個1px或2px的細白色高光線,一旦UIToolbar結束(在「PM」中的「M」中間),就會變成灰色,我希望它看起來像我的NavBar的其餘部分。這兩個酒吧風格的設置爲「默認」(UIBarStyleDefault)。 – buley 2010-05-09 19:33:32

0

而不是把按鈕到工具欄,你可能只是讓他們的孩子的意見父UIView的。您可以直接通過其framecenter屬性定位按鈕,而無需使用墊片等。

+0

這是有點在我的頭上,但我會研究這一點。感謝一如既往亞歷克斯,你是非常有幫助的 – buley 2010-05-09 19:25:30

+0

我實際上做這個確切的事情我的一個應用程序正在開發中。:) – 2010-05-09 19:36:33

+0

這種方法的唯一問題是您需要獲取自己的圖像,您不能使用內置的'UIBarButtonSystemItem' – 2010-05-09 19:38:09

相關問題