iOS7 UI相容 導航欄按鈕邊框 UINavigationItem left and right padding

LeexMuller發表於2013-12-03

ios7中得導航欄按鈕相對於ios7之前的版本會出現12個畫素左右的偏移,這使得很多UI不是那麼協調了。為了解決一個問題,

重寫UINavigationItem的setLeftBarButtonItem和setRightBarButtonItem方法,使之與之前版本相容;

程式碼如下:

@interface UINavigationItem (margin)

@end

@implementation UINavigationItem (margin)

if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

  • (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
    {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = -12;

    if (_leftBarButtonItem)
    {
        [self setLeftBarButtonItems:@[negativeSeperator, _leftBarButtonItem]];
    }
    else
    {
        [self setLeftBarButtonItems:@[negativeSeperator]];
    }
    [negativeSeperator release];
    

    }
    else
    {
    [self setLeftBarButtonItem:_leftBarButtonItem animated:NO];
    }
    }

  • (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
    {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = -12;

    if (_rightBarButtonItem)
    {
        [self setRightBarButtonItems:@[negativeSeperator, _rightBarButtonItem]];
    }
    else
    {
        [self setRightBarButtonItems:@[negativeSeperator]];
    }
    [negativeSeperator release];
    

    }
    else
    {
    [self setRightBarButtonItem:_rightBarButtonItem animated:NO];
    }
    }

endif

@end

專案原始碼不做任何修改,問題解決;

新增Button:

UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = leftBarItem;
[leftBarItem release];

博文地址:http://www.cnblogs.com/maxfong/p/3375167.html

相關文章