屏幕的某些部分在SWIFT中无法单击 [英] Some part of the screen is not clickable in SWIFT

查看:84
本文介绍了屏幕的某些部分在SWIFT中无法单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器(横向模式)下有一个嵌套的UIview,其中包含2个按钮。
第一个按钮位于屏幕的左侧,第二个按钮位于最右侧。我只是想知道为什么第二个按钮只有1/4的部分有效。我将按钮重新排列在0轴上,并且效果很好。

I have a nested UIview under my view controller (landscape mode) that holds 2 buttons. The first button is placed on the left side of the screen and the second button is located at the right most. I'm just wondering why only 1/4 part of the 2nd button is working. I re-arranged that button on the 0 axis and it worked well.

我认为我在uiview上传递的帧有问题。
请参阅我的代码。

I think there's something wrong on the frame that I passed on the uiview. Please see my code.

导入UIKit

ViewController类:UIViewController {

class ViewController: UIViewController {

var screenHolder:ScreenHolder?
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    ProjectData.currentFrame = self.view.frame
    screenHolder = ScreenHolder(frame: ProjectData.currentFrame)
    self.view.addSubview(screenHolder!)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

推荐答案

此类问题的通常原因是子视图完全或部分在其父视图的范围之外。发生这种情况时,子视图将保持可见状态,但无法对其进行更改。这是因为命中测试的工作方式-它从超级视图向下传播,因此看不到超级视图外的抽头,因此不会针对任何子视图进行测试。

The usual reason for this sort of problem is that the subview is totally or partially outside the bounds of its superview. When that happens, the subview remains visible but it becomes un-tappable. This is because of the way hit-testing works - it propagates down from the superview, so taps outside the superview are not seen and thus are not tested against any subviews.

因此,在这种情况下,您说:

So, in this case, you say:


在我的具有2个按钮的视图控制器(横向模式)下的嵌套UIview

nested UIview under my view controller (landscape mode) that holds 2 buttons

其中一个按钮(子视图)在嵌套的UIView(超级视图)之外。

One of the buttons (subview) is outside the nested UIView (the superview).

检测/调试这种情况的最佳方法是将超级视图的 clipsToBounds 设置为 true 。这样,如果子视图完全或部分在子视图之外,您将不能看到它,因此您将了解发生了什么,并且不会因子视图的可见性而被误导。

The best way to detect / debug this sort of situation is to set the superview's clipsToBounds to true. That way, if a subview is totally or partially outside it, you will not be able to see it, and so you will understand what has happened and you will not be misled by the visibility of the subview.

这篇关于屏幕的某些部分在SWIFT中无法单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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