在iOS 11中,按钮和文本字段作为UIToolBar的子视图没有响应。将视图层次结构与iOS 10进行比较,我们可以看到在UIToolBar的所有子视图上都有一个_UIToolBarContentView。
例如,这个UIToolBar的新布局破坏了slacktextviewcontroller https://github.com/slackhq/SlackTextViewController/issues/604
需要一个在iOS 10/11中工作的解决方案。
发布于 2017-09-27 13:08:15
为了解决iOS11 (兼容较低版本)的问题,您只需要在将layoutSubview作为子视图添加到UI层次结构之后立即生成UIToolBar。
在这种情况下,_UIToolbarContentView会降低到UIToolBar的第一个子视图,并且可以像以前一样将所有子视图添加到更高的位置。
例如在ObjC中,
UIToolbar *toolbar = [UIToolbar new];
[self addSubview: toolbar];
[toolbar layoutIfNeeded];
<here one can add all subviews needed>同样的问题也发生在松弛文本视图控制器上。
发布于 2017-10-25 03:04:30
我在我的案子中解决了这个问题。我在layoutSubviews的子类中重写了UIToobar方法,并将userInteractionEnable of _UIToolbarContentView转换为NO。
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *subViewArray = [self subviews];
for (id view in subViewArray) {
if ([view isKindOfClass:(NSClassFromString(@"_UIToolbarContentView"))]) {
UIView *testView = view;
testView.userInteractionEnabled = NO;
}
}
}发布于 2017-10-03 13:59:46
您可以只使用hitTest(_:with:)方法。
contentView中创建一个属性UIToolbar:
打开私有(Set) var contentView: UIView = UIView()contentView的框架与UIToolbar的相同。例如:
contentView.frame =界contentView.autoresizingMask = .flexibleWidth,.flexibleHeight addSubview(contentView)hitTest(_:with:)方法:
打开覆盖函数hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView?{ if self.point(内部: point,with: event) { if = contentView.hitTest(point,with: event) {返回hitTestView }if{返回自}或{ return }}在这种情况下,如果您想通过简单地添加其他视图来自定义工具栏,则应该将它们添加到contentView中,以便将它们正确定位。
https://stackoverflow.com/questions/46107640
复制相似问题