我想通過工具欄的幫助來定製我的NavigationBar。 我如下編程來實現它:UIToolbar精確尺寸
UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];
,然後我把它添加到我的導航欄。問題是,我對這個邊界醜的效果:
我試圖改變y和高度值,但沒有任何結果。 你有什麼想法可以避免這種情況嗎?
在此先感謝,yassa
我想通過工具欄的幫助來定製我的NavigationBar。 我如下編程來實現它:UIToolbar精確尺寸
UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];
,然後我把它添加到我的導航欄。問題是,我對這個邊界醜的效果:
我試圖改變y和高度值,但沒有任何結果。 你有什麼想法可以避免這種情況嗎?
在此先感謝,yassa
我部分同意以前的答案和評論。 您建議的解決方案適用於自定義按鈕。但是如果我想實現標準的編輯按鈕呢? 訪問標準按鈕/圖標是通過UIBarButtonItem類,而不是UIButton。而且你不能將UIBarButtonItem對象添加到UIView。
在網絡上進行了很多研究之後,我發現了完全覆蓋我的需求的解決方案。工具欄必須按以下方式創建:
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
tools.tintColor = self.navigationController.navigationBar.tintColor;
tools.barStyle = -1;
這是結果:
希望它能幫助! yassa
我不會這樣做。 您可以通過向navigationItem.rightBarButtonItem添加帶有2個按鈕的視圖來實現相同的效果。它很簡單:
// view that will hold the buttons
UIView* container = [[UIView alloc] init];
// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];
//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];
// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;
+1正確可靠的處理方式 – Till
或者你可以這樣做。 只需創建UIToolbar的新的子類這樣
@interface MyToolbar : UIToolbar
@end
@implementation MyToolbar
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.clearsContextBeforeDrawing = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// do nothing
}
- (void)dealloc {
[super dealloc];
}
@end
而且使用它作爲正常UIToolbar。我不知道爲什麼,但它只是起作用。
您正在添加一個工具欄到導航欄? – Till
是的,我在一些教程中發現了這種方法......這是一種錯誤的方法嗎? – yassassin
是的,看到shannogans的答案 - 這是「正確的」方式 – Till