2012-10-14 35 views
1

我想重現像導航欄Tweetbot。
我仍在搜索是否可以在UINavigationBar之間放置UIButton來代替標題,以使它完全適合左右按鈕,就像在Tweetbot應用程序中完成的那樣。
當我嘗試這樣做,過了一定規模的self.navigationItem.titleView被調整下來自動像在TweetBot navigaton酒吧UIButton

告訴我,如果我錯過了什麼明顯的,
謝謝

我提供兩個截圖,讓你看看我米聊關於 Button normal
Button hihlighted

回答

1

他們可能已經推出自己的執行UINavigationBar和/或UINavigationController。我想這不值得嘗試破解UIKit,因爲它會非常脆弱,而且不會面向未來。

+0

脆弱性和麪向未來的想法並不是我想到的。我仍然在尋找 – gsempe

+0

我認爲你不需要走那麼遠。子類化這些類應該是最後的解決方案。 – nverinaud

0

你可以添加一個按鈕到導航項的標題視圖屬性,該屬性接受一個UIView,並且因爲UIButton是UIView的子類,所以這不是問題。

或者,如果您正在使用故事板,只需將一個按鈕拖到導航欄的中心,就很容易。

// Create the button, and specify the type 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

// Set the button's frame 
button.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f); 

// Set your custom image for the button 
[button setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME"] forState:UIControlStateNormal]; 

// Set the button's action, which is the method that will be called when it is pressed 
[button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside]; 

// Add the button to the navigation bar's title view 
[self.navigationItem.titleView addSubview:button]; 

SDK參考: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html

+0

添加一個按鈕作爲titleView的子視圖不是問題。問題是要使它完全適合左右按鈕 – gsempe

+0

@gsempe我自己沒有應用程序,但我不明白爲什麼你不能動態地調整按鈕的框架。該按鈕已經居中在標題視圖中,根據需要調整寬度 – Vikings

+0

@gsempe它看起來像一些Photoshop技巧 – Vikings

0

你有沒有嘗試autoresizesSubviews設置爲YES上titleview的,然後設置在UIButton的正確自動尺寸口罩?如果這不起作用,我建議您創建自定義視圖子類並覆蓋-layoutSubviews以適應您的需求。