iOS NavigationBar点击混乱问题
2016-09-19 本文已影响82人
_YZG_
使用系统的navigationBar的时候,它下面如果有其他控件,对这些控件进行触摸时,往往响应的时navigationBar。
这是因为iPhone自带的控件NavigationBar上的Button、TabBar上的Item、ToolBar上的ButtonItem,实际的点触区域都是被放大了的。
虽然iPhone导航栏上的操作按钮仅有29px高,但是它的实际点触区域比整个导航栏的高度还要高出5px左右,大概能达到44px+5px。
解决方案
1.自定义navigationBar,Foursquare和USA today等应用最后都是采用自定义TabBar的方式规避问题的。
2.去掉navigationBar的多出的5px的触屏事件,并给出该5px应该对应的控件触屏事件
3.让点击的位置为实际位置,不要漂移(相对上面解决方案要简单很多,但局限于企业内发布的App)
方法二
去掉navigationBar多出5px区域的交互:新建一个CustomNavigationBar继承UINavigationBar重写hitTest方法
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
self.userInteractionEnabled = YES;
} else {
self.userInteractionEnabled = NO;
}
return [super hitTest:point withEvent:event];
}
添加子类CustomNavigationBar到NavigationControl
- (id) initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
if (self) {
self.viewControllers = [NSArray arrayWithObjects:rootViewController, nil];
}
return self;
}
- 到这就做完了,但是原文还要搞个UIWindow重写sendEvent方法,但是做完这两部好使了,到时候再说吧,有朋友知道的话欢迎指正,参考地址在最后
方法三
新建CustomWindow继承UIWindow,重写warpPoint方法,让点击的位置不漂移。
@implementation CustomWindow
-(CGPoint)warpPoint:(CGPoint)point;
{
return point;//因为点击的时候位置漂移,导致navigationbar点击混乱。所以在此返回point,让点击某位置即为该位置的坐标,不漂移
}
@end
- 但是如果你的app需要发布到App Store,因为warpPoint是私有方法,会被App Store拒绝掉。所以重写warpPoint的这种解决方案只针对企业内部的App使用