美文网首页iOS技术点
iOS 导航栏按钮自定义,滑动手势生效

iOS 导航栏按钮自定义,滑动手势生效

作者: 姜流儿96 | 来源:发表于2016-10-13 10:06 被阅读176次

以返回按钮为例。返回按钮自定义之后位置有些偏右,因为导航栏的NavigationItem是个比较特殊的View,所以我们无法通过简单的调整Frame来的调整左右按钮的位置。可以使用苹果提供的UIButtonBarItem中有个叫做UIBarButtonSystemItemFixedSpace的控件调整位置。

UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(0, 0, 20,20);
[leftBtn setBackgroundImage:[UIImage imageNamed:@"图片名称"] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
//创建UIBarButtonSystemItemFixedSpace
UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//将宽度设为负值spaceItem.width = -10;
//将两个BarButtonItem都返回给NavigationItem
self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];

手势生效
如果自定义导航栏按钮会使滑动手势失效。只要重新添加导航栏的interactivePopGestureRecognizerdelegate手势即可回归。

self.navigationController.interactivePopGestureRecognizer.delegate = self;

相关文章

网友评论

    本文标题:iOS 导航栏按钮自定义,滑动手势生效

    本文链接:https://www.haomeiwen.com/subject/jprwyttx.html