更新iOS 11之后,用xcode 9运行App,你会发现导航栏返回按钮偏移20像素
下面我讲一下解决办法:
楼主写的分类:UIViewController+BarButton
代码如下:
//左侧一个图片按钮的情况
- (void)addLeftBarButtonWithImage:(UIImage *)image action:(SEL)action
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
view.backgroundColor = [UIColor clearColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(0, 0, 44, 44);
[firstButton setImage:image forState:UIControlStateNormal];
[firstButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, 5 * kScreenWidth / 375.0, 0, 0)];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:firstButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
//右侧一个图片按钮的情况
- (void)addRightBarButtonWithFirstImage:(UIImage *)firstImage action:(SEL)action
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
view.backgroundColor = [UIColor clearColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(0, 0, 44, 44);
[firstButton setImage:firstImage forState:UIControlStateNormal];
[firstButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5 * kScreenWidth / 375.0)];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:firstButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
//右侧为文字item的情况
- (void)addRightBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *rightbBarButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 88, 44)];
[rightbBarButton setTitle:itemTitle forState:(UIControlStateNormal)];
[rightbBarButton setTitleColor:kBlackColor forState:(UIControlStateNormal)];
rightbBarButton.titleLabel.font = [UIFont systemFontOfSize:14];
[rightbBarButton addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
rightbBarButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5 * kScreenWidth/375.0)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightbBarButton];
}
//左侧为文字item的情况
- (void)addLeftBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *leftbBarButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
[leftbBarButton setTitle:itemTitle forState:(UIControlStateNormal)];
[leftbBarButton setTitleColor:kBlackColor forState:(UIControlStateNormal)];
leftbBarButton.titleLabel.font = [UIFont systemFontOfSize:16];
[leftbBarButton addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
leftbBarButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[leftbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5 * kScreenWidth/375.0, 0, 0)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftbBarButton];
}
//右侧两个图片item的情况
- (void)addRightTwoBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
view.backgroundColor = [UIColor clearColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(40, 0, 40, 44);
[firstButton setImage:firstImage forState:UIControlStateNormal];
[firstButton addTarget:self action:firstAction forControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, - 8 * kScreenWidth/375.0)];
[view addSubview:firstButton];
UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(0, 0, 40, 44);
[secondButton setImage:secondImage forState:UIControlStateNormal];
[secondButton addTarget:self action:secondAction forControlEvents:UIControlEventTouchUpInside];
secondButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, - 15 * kScreenWidth/375.0)];
[view addSubview:secondButton];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
//右侧三个图片item的情况
- (void)addRightThreeBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction thirdImage:(UIImage *)thirdImage thirdAction:(SEL)thirdAction
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
view.backgroundColor = [UIColor clearColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(80, 0, 40, 44);
[firstButton setImage:firstImage forState:UIControlStateNormal];
[firstButton addTarget:self action:firstAction forControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, - 8 * kScreenWidth/375.0)];
[view addSubview:firstButton];
UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(44, 0, 40, 44);
[secondButton setImage:secondImage forState:UIControlStateNormal];
[secondButton addTarget:self action:secondAction forControlEvents:UIControlEventTouchUpInside];
secondButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, - 5 * kScreenWidth/375.0)];
[view addSubview:secondButton];
UIButton *thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
thirdButton.frame = CGRectMake(0, 0, 40, 44);
[thirdButton setImage:thirdImage forState:UIControlStateNormal];
[thirdButton addTarget:self action:thirdAction forControlEvents:UIControlEventTouchUpInside];
[thirdButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, - 5 * kScreenWidth/375.0)];
[view addSubview:thirdButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}
截图.png
使用方法:在viewDidLoad里直接调用
[self addLeftBarButtonWithImage:[UIImage imageNamed:@"点赞"] action:@selector(leftBarbuttonAction)];
如果觉得坐标有问题,可以去UIViewController+BarButton里更改。
注意:以上我写的方法,位置已经不会偏移了。
但是会出现item点击不准确的情况。借鉴了UINavigation-SXFixSpace的几个分类,解决了我的问题。谢谢此位大哥的分享。
网友评论