2013-03-07 80 views
1

出於某種原因,當我設置爲uibarbutton項目自定義背景圖片後的UIBarButtonItem自定義背景外觀「跳」:翻動過渡

UIImage *button30 = [[UIImage imageNamed:@"nav_bar_button_orange.png"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; 
[[UIBarButtonItem appearance] setBackgroundImage:button30 forState:UIControlStateNormal 
             barMetrics:UIBarMetricsDefault]; 

我的按鈕是「跳躍」或更新它的寬度(或者更準確的在文本的每一邊填充)水平翻轉。

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain handler:^(id sender) { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
}]; 

忽略處理程序位,我正在使用BlocksKit。

你可以看到它發生在這裏:http://screencast.com/t/HZRBS70OT6wt

附加信息

它甚至做到這一點的時候,我把故事板的,而不是代碼到位的按鈕。似乎只發生在水平翻轉。

原因:

顯然它是與使用自定義字體。幾乎就像它一開始沒有計算寬度。

示例項目

Recreated the problem in this sample project。如果有人知道如何解決這個問題,會很樂意。不能相信沒有人遇到它。

+0

你在哪裏運行self.navigationItem.leftBarButtonItem代碼,在viewDidLoad中? – 2013-03-07 02:02:02

+0

@PatrickTescher是在viewDidLoad中。 – 2013-03-07 03:12:14

+0

它幾乎看起來像它在後退按鈕和常規按鈕之間切換。也許嘗試seding setBackgroundVerticalPositionAdjustment:forBarMetrics: – 2013-03-07 18:30:44

回答

4

我不知道這種行爲的原因,但設置圖像插入有助於解決這個問題。 enter image description here

+0

真棒引入的外觀定製的東西。這樣可行。如果有人知道原因並提供更好的解決方案,請稍後再打開,否則我會標記它。謝謝! – 2013-03-07 21:50:38

0

無論何時您需要在bar按鈕中進行UI修改,始終最好使用自定義視圖進行初始化。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:button30 forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(dismissFuction:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 
+0

使用UIBarButtonItems是完全可以接受的。你不需要爲一切添加按鈕。 – 2013-03-07 01:59:15

+0

我也知道這一點,但UIBarbutton是不是很開放定製 – 2013-03-07 04:48:12

+0

豐富的可定製。查看iOS5 – 2013-03-07 18:21:27