问题
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.
I think there's something wrong on the frame that I passed on the uiview. Please see my code.
import UIKit
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.
}
}
回答1:
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:
nested UIview under my view controller (landscape mode) that holds 2 buttons
One of the buttons (subview) is outside the nested UIView (the superview).
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.
回答2:
Here's the solution for those who has the same problem :)
import UIKit
class ViewController: UIViewController {
var screenHolder:ScreenHolder?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
println("bounds \(view.bounds)")
ProjectData.currentFrame = self.view.bounds
screenHolder = ScreenHolder(frame: ProjectData.currentFrame)
self.view.addSubview(screenHolder!)
screenHolder?.clipsToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
来源:https://stackoverflow.com/questions/27832907/some-part-of-the-screen-is-not-clickable-in-swift