当它成为UIView其他self.view的子视图时,UISlider将无法工作 [英] UISlider won't work when it becomes a subview of a UIView other self.view

查看:385
本文介绍了当它成为UIView其他self.view的子视图时,UISlider将无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将滑块添加为除self.view之外的任何视图的子视图时,它不起作用(不滑动)但是当它是self.view的子视图时它工作正常。除了self.view之外,您可以在其他视图中看到它。但它不起作用。

When I add my slider as subview of any view besides self.view it does not work (doesn't slide) but it works fine when it is a subview of self.view. You can see it on the other views besides self.view but it doesn't work.

这是我的代码:

alphaSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 90, 10)];
    [alphaSlider setMinimumValue:0.01];
    [alphaSlider setMaximumValue:1];
    [alphaSlider setValue:0.5];
    [alphaSlider setUserInteractionEnabled:YES];
    [alphaSlider addTarget:self action:@selector(alphaSliderDidChange:) forControlEvents:UIControlEventValueChanged];
    alphaSlider.continuous = YES;
[submenu addSubview:alphaSlider];

有什么方法可以解决这个问题吗?

Any way to fix this?

推荐答案

可能是子菜单未启用用户交互。

It may be that submenu is not enabled for user interaction.

[submenu setUserInteractionEnabled:YES];

另一种可能性是滑块的框架在submenu.frame之外

Another possibility is that the slider's frame is outside of submenu.frame

由于UIViews的clipsToBounds属性默认为NO,因此滑块不会被剪裁,尽管它不在子菜单的框架之外。这意味着子菜单的框架不会覆盖滑块,并且无法将子菜单中的触摸事件传递到子菜单的滑块。设置子菜单的背景颜色并确认滑块完全位于子菜单的框架中。

Since the clipsToBounds property of UIViews is by default NO, the slider would not be clipped despite being outside of submenu's frame. This means that submenu's frame doesn't cover the slider, and there's no way to pass touch events from submenu to submenu's slider. Set the background color of submenu and confirm that the slider is positioned entirely in submenu's frame.

[submenu setBackgroundColor:[UIColor blueColor]];

子菜单的框架可能类似于(0,0,0,0)。

Submenu's frame might be something like (0, 0, 0, 0).

包含

NSLog(@"%f, %f, %f, %f", submenu.frame.origin.x, submenu.frame.origin.y, submenu.frame.size.width, submenu.frame.size.height);

你也可以这样做

[submenu setClipsToBounds:YES];

如果滑块消失,则子菜单的框架不好。

and if the slider disappears, then submenu's frame is bad.

这篇关于当它成为UIView其他self.view的子视图时,UISlider将无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆