推荐一篇非常好的集成各种UITabBar的三方库 《点击这里直取demo》
另外一篇根据runtime定制了一款可以出轨的UITarBar 《Runtime实战之定制TabBarItem大小》
点击view的触发机制《iOS事件分发机制(一) hit-Testing》
摘自:
UIView中提供两个方法用来确定hit-testing View,如下所示 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds
当一个View收到hitTest消息时&#xff0c;会调用自己的pointInside:withEvent:方法,如果pointInside返回YES&#xff0c;则表明触摸事件发生在我自己内部&#xff0c;则会遍历自己的所有Subview去寻找最小单位(没有任何子view)的UIView&#xff0c;如果当前View.userInteractionEnabled &#61; NO,enabled&#61;NO(UIControl),或者alpha<&#61;0.01, hidden等情况的时候&#xff0c;hitTest就不会调用自己的pointInside了&#xff0c;直接返回nil&#xff0c;然后系统就回去遍历兄弟节点。简而言之&#xff0c;可以写成这样
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {if (self.alpha <&#61; 0.01 || !self.userInteractionEnabled || self.hidden) {return nil;}BOOL inside &#61; [self pointInside:point withEvent:event];UIView *hitView &#61; nil;if (inside) {NSEnumerator *enumerator &#61; [self.subviews reverseObjectEnumerator];for (UIView *subview in enumerator) {hitView &#61; [subview hitTest:point withEvent:event];if (hitView) {break;}}if (!hitView) {hitView &#61; self;}return hitView;} else {return nil;}
}