1

我創建了一個自定義UINavigationBar,它比蘋果默認導航欄稍高一點。向下移動UIBarButtonItem幾個像素?

我似乎無法找到一種方法將UIBarButtonItem向下移動,直接在兩條虛線之間居中。

是否有一個簡單的方法來做到這一點?我試過創建一個自定義按鈕,但沒有成功。理想情況下,我只想將默認後退按鈕向下移動幾個像素。

enter image description here

代碼用於創建UINavigationBar,自定義首部的圖像和UIBarButtonItem:預先

//Create image for navigation background 
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"navbackground.png"] 
             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground 
            forBarMetrics:UIBarMetricsDefault]; 

//Centered title image 
UIImageView *headerImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"hometitle.png"]]; 
    [headerImage setFrame: CGRectMake(0, 0, 180, 49)]; 
self.navigationItem.titleView = headerImage; 

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init]; 
[backBarButtonItem setTintColor: [UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]]; 
[backBarButtonItem setStyle: UIBarButtonItemStylePlain]; 
    self.navigationItem.backBarButtonItem = backBarButtonItem; 
[backBarButtonItem release]; 

謝謝,

+0

也許改變虛線更容易? – Pfitz

+0

如果這是唯一的解決方案,那麼我可能會這樣做。稍等片刻,看看其他人是否還有其他建議。 –

+0

你是如何創建自定義的UINavigationBar並將UIButton添加到它的? – StuStirling

回答

3

創建使用定製視圖的的UIBarButtonItem。這個自定義視圖將是一個UIView,其中實際的UIButton(作爲子視圖)放置在頂部x像素(x =您想要將其向下移動的像素數)。

-1

對不起

UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; myButton1.showsTouchWhenHighlighted = YES; 
myButton1.frame = CGRectMake(0.0, 3.0, 50,30); 
    [myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];  
self.navigationItem.leftBarButtonItem = leftButton; 
+0

得到一個警告 - 「 '的UIBarButtonItem' 可不迴應 'SETFRAME:'」,它會導致程序崩潰 –

+0

對不起 的UIButton * myButton1 = [UIButton的buttonWithType:UIButtonTypeCustom]; [myButton1 setImage:[UIImage的imageNamed。 :@ 「back.png」] forState:UIControlStateNormal]; myButton1.showsTouchWhenHighlighted = YES; myButton1.frame = CGRectMake(0.0,3.0,50,30); [myButton1 addTarget:自動作:@selector(背面) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1]; self.navigationItem.leftBarButtonItem = left按鈕; –

相關問題