I have a UILabel
on both sides of a UIPageControl
, as pictured below:
I found that tapping on the sides of the UIPageControl
the dot would progress, ie not on my arrows and not triggering a method of mine. But nothing else would change, so I set isUserInteractionEnabled
to false
on the UIPageControl
.
I connected (via UITapGestureRecognizer
) another UILabel
above and it launches a method fine.
However the UILabel
s, beside the UIPageControl
, will not work. NOTE: I do have isUserInteractionEnabled
set to true
on this element. (I even temporarily changed the element to a UIButton
and it wouldn't work either - so I reverted back to my UILabel
.)
So, is there a way to add a UILabel
to the side of a UIPageControl
that gets triggered? Alternately, can the invisible objects (while tapping on the left or right of the UIPageControl
- like in my second sentence) be connected to a custom method?
UPDATE:
I've added another separate modal (same title label, buttons, but no collection view and no page control). Again it won't respond when I press the label in the centre, but I temporarily am using the title label which responses and launches my method fine.
Can anyone say why?
I solved the issue!
I was declaring most variables - as well as the UILabel - in closures blocks, at the top of the class, like below:
let myLabel: UILabel = {
{
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
view.isUserInteractionEnabled = true
view.textColor = UIColor.blue
return view
}
Within this closure I had view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(doMyLabelMethod(_:))))
and view.isUserInteractionEnabled = true
, when I moved them out of the closure and placed them directly above addSubview(myLabel)
it fired fine.
So variable-closures are good to contain many dressing, but not gesture recognizers!