iOS导航栏BarButtonItem间距(item之间以及与屏

2018-02-07  本文已影响108人  ZYiDa

如下代码 是我在自定义WKWebView浏览器时用到的,这里是Github地址,有需要的可以看一下。

#pragma mark 设置BarButtonItem
- (void)setBarButtonItem
{
    //通过imageInset调整item的位置和item之间的位置
    //设置返回按钮
    self.leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:[self addItemWithImage:@"back_item"
                                                                               imageInset:UIEdgeInsetsMake(0, -10, 0, 10)
                                                                                      size:CGSizeMake(28, 28)
                                                                                    action:@selector(selectedToBack)]];

    //设置关闭按钮
    self.leftBarButtonSecond = [[UIBarButtonItem alloc]initWithCustomView:[self addItemWithImage:@"close_item"
                                                                                     imageInset:UIEdgeInsetsMake(0, -15, 0, 15)
                                                                                            size:CGSizeMake(28, 28)
                                                                                          action:@selector(selectedToClose)]];

    self.navigationItem.leftBarButtonItems = @[self.leftBarButton];


    if (@available(iOS 11,*)) {
        NSLog(@"** iOS 11 **");
    }else{
        NSLog(@"** Not iOS 11 **");
    }

    //设置刷新按妞
    UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reload_item"]
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(selectedToReloadData)];
    self.navigationItem.rightBarButtonItems = @[reloadItem];

}

#pragma mark 添加item
- (UIButton *)addItemWithImage:(NSString *)imageName imageInset:(UIEdgeInsets)inset size:(CGSize)itemSize action:(SEL)action {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:imageName];
    button.frame = CGRectMake(0, 0, itemSize.width, itemSize.height);
    [button setImageEdgeInsets:inset];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:16];

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = rightItem;

    return button;
}

不足的地方,还请各位多多指教,谢谢了。

上一篇下一篇

猜你喜欢

热点阅读