iOS8 自定義navigationbar 以及 UIBarButtonItem 邊距問題 – iPhone手機開發 iPhone軟體開發教學課程

一。自定義navigationbar

 

復制代碼

- (void)initNavigationBar{

    [self.navigationController setNavigationBarHidden:YES];

    UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 32)];

    [bar setBackgroundImage:[UIImage imageNamed:@zhuche_bar2.png] forBarMetrics:UIBarMetricsDefault]; 

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:nil];

    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];

    [left setFrame:CGRectMake(0, 2, 28, 28)];

    [left setImage:[UIImage imageNamed:@zhuche_back.png] forState:UIControlStateNormal];

    [left addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:left];

    [item setLeftBarButtonItem:leftButton];

    [bar pushNavigationItem:item animated:NO];

    [self.view addSubview:bar];

}


- (void)back{

    [self.navigationController popViewControllerAnimated:YES];

}

復制代碼

雖然可以解決自定義navigationbar的問題,左右按鈕都可以替換,但是如果放在UITableViewController中,自定義的navigationbar會隨著scrollview的滑動而滑動,不會像系統自帶的navigationbar一樣始終保持在屏幕最上方。那麼我們可以有第二種解決方式,修改原來的navigationbar,其實就是改一下leftBarButtonItem或者rightBarButtonItem在navigationbar上面的位置:修改UIBarButtonItem 的邊距。

二。修改UIBarButtonItem 的邊距

復制代碼

@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 = -16;//此處修改到邊界的距離,請自行測試
        
        if (_leftBarButtonItem)
        {
            [self setLeftBarButtonItems:@[negativeSeperator, _leftBarButtonItem]];
        }
        else
        {
            [self setLeftBarButtonItems:@[negativeSeperator]];
        }
       
        
    }
    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]];
        }
        
    }
    else
    {
        [self setRightBarButtonItem:_rightBarButtonItem animated:NO];
    }
}

#endif
@end

復制代碼

以上代碼可以放置在主視圖控制器的.m文件裡面,@implementation ViewController的上面。

 

You May Also Like