最近在做项目的过程中发现,在iOS11中点击Toolbar上的按钮没有响应,所以贴出解决方案如下,进行记录。
出现原因:Toolbar 在iOS11默认添加有_UIToolbarContentView,_UIToolbarContentView
_UIButtonBarStackView覆盖在自定义的按钮上面,导致按钮无响应。
Toolbar 图层如下:


解决方案:
在UIToolBar被添加为UI层次结构的子视图之后,调用 layoutIfNeeded方法,UIToolbarContentView会降低到UIToolBar的第一个子视图,然后就可以将所有的子视图添加到最顶层。
代码如下:
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,35)];
self.inputAccessoryView = toolbar;
[toolbar layoutIfNeeded];
NSArray * subViewArray = [toolbar subviews];
for (id view in subViewArray) {
if ([view isKindOfClass:(NSClassFromString(@"_UIToolbarContentView"))]) {
UIView *testView = view;
testView.userInteractionEnabled = NO;
}
}
网友评论