iOS7 UI兼容 导航栏按钮边框 UINavigationItem left and right padding

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

    原文作者:LeexMuller
    原文地址: https://segmentfault.com/a/1190000000350406
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞