I'm hitting a wall. I have a UIScrollView with a child UIView (contentView) which contains the content and a button. The button is not detecting any taps. I've tested many methods, creating the button programmatically, using storyboard, and creating a UITapGestureRecognizer in the contentView... still nothing. From what I understand, UIScrollView does not delegate any touch events to it's children. How do I solve this?
You can create the button programmatically then you add it as a subview for your contentView.
let button = UIButton()
button.frame = CGRectMake(contentView.center.x-20.0, contentView.center.y-20.0, 40.0, 40.0)
button.addTarget(self, action: "handleTap", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("Title", forState: UIControlState.Normal)
contentView.addSubview(button)
On the action you can handle the tap
func handleTap() {
//Do whatever you want
print("Button tapped")
}
I tested it using a scrollView and a UIView inside the scrollView
Related
I made a UIScrollView and add 2 SubViews.(View1, View2)
So, I can page down if i swipe because i set .isPagingEnabled = true.
And now I make a one button in View1.
What I want to do is change View2 when i click this button.
To be specific, change view by paging mode.(not like scrolling)
button.addTarget(self, action: #selector(downBtnTapped), for: .touchUpInside)
#objc func downBtnTapped() {
if verticalScroll.contentOffset.y < self.view.bounds.height * CGFloat(2) {
verticalScroll.contentOffset.y += self.view.bounds.height
}
}
if i tried upper code, just chnage view directly, not like paging Mode
How can i solve this problem ?
There are two custom views - viewA and viewB.ViewB is a small view added in viewA. At first viewA is filled in the whole screen. ViewB is located in the bottom of viewA(out of the screen). When click a button in viewA, viewB bottom constant of constraints will be -100, thus viewB will display in the bottom of the screen. But there is a button in viewB did not response its selector. Here is my code:
In ViewA
let viewB: ViewB = {
let view = ViewB(
view.setup()
return view
}()
viewB constraints:
viewB.snp.makeConstraints { (make) in
make.trailing.leading.equalTo(self)
make.height.equalTo(100)
make.top.equalTo(self.snp.bottom)
}
when click button to arouse viewB
UIView.animate(withDuration: 0.5) {
self.snp.updateConstraints({ (make) in
make.bottom.equalTo(-100)
})
}
self.layoutIfNeeded()
In viewB:
class ViewB: UIView {
let b: UIButton = {
let button = UIButton(type: UIButtonType.custom)
button.addTarget(self, action: #selector(btnClicked), for: UIControlEvents.touchUpInside)
button.backgroundColor = UIColor.green
button.setTitle("Button", for: .normal)
return button
}()
func btnClicked() {
print("btnClicked")
}
func setup() {
addSubview(b)
b.snp.makeConstraints { (make) in
make.leading.top.bottom.equalTo(self)
make.width.equalTo(100)
}
}
}
Your code by itself works fine. So the issue is probably something like the button being overlaid by another view which takes the touch actions. You can test this using the view debugger or provide a link to your project so one of us can take a look to see what is going on.
i think , your view is going out of its super View or Any other view is coming on its upper layer. please see by this
superview.clipsToBounds : YES
I'm following a tutorial online and I'm trying to add touch detection to an UIImageView. I realize that this can be done with a gesture recognizer but I have a different problem and I don't know how to ask for it.
Basically, I added an overlay UIButton initialized from the frame of imageView. However, I have to move the frame down 64 px because of the navigation bar. My imageView is added form the storyboard but UIButton is added programmatically.
Is there a way to do this without adding 64 px manually? This is no biggies but since the frame of the imageView when printing is (10, 10, 355, 450) and the frame of UIButton is (10, 74, 355, 450), it kinda ticks me off a little bit.
Let me clarify, I'm wondering why view controller doesn't take into account the size of the navigation bar when adding subview programmatically? It seems to handle this just fine when adding subview from Storyboard.
Without adding 64px:
overlay = UIButton(frame: imageView.frame)
overlay.backgroundColor = UIColor.red
overlay.addTarget(self, action: #selector(imageViewTapped), for: .touchUpInside)
view.addSubview(overlay)
With 64px added - fits perfectly
overlay = UIButton(frame: imageView.frame.offsetBy(dx: 0, dy: 64))
overlay.backgroundColor = UIColor.red
overlay.addTarget(self, action: #selector(imageViewTapped), for: .touchUpInside)
view.addSubview(overlay)
You can add button on image view.
Try This :
let overlay = UIButton(frame: imageView.bounds)
imageView.isUserInteractionEnabled = true
overlay.backgroundColor = UIColor.red.withAlphaComponent(0.3)
overlay.addTarget(self, action: #selector(imageViewTapped), for: .touchUpInside)
imageView.addSubview(overlay)
You can use UITapGestureRecognizer for the same as given below.
let bg = UIImageView(image: UIImage(named: "Home"))
bg.frame = self.view.frame
self.view.addSubview(bg)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(SecondViewController.onTapScreen))
bg.isUserInteractionEnabled = true
bg.addGestureRecognizer(tapRecognizer)
Handle tap event as given below
func onTapScreen() {
// Handle touch here.
}
By this way you don't need to use button also so it can avoid multiple controls.
Set your UIViewController's navigationbar to Opaque Navigation Bar. It will start you view below navigation bar.
I'm constantly running into this issue, I don't know how to treat UITableViewCells as UIViews.
I added a button to my UITableViewCell:
let btnWidth = self.contentView.frame.size.width * 1.1
let btnHeight = self.contentView.frame.size.height * 1.6
btnJoinChannel = UIButton(frame: CGRect(x:0,y:0,width:btnWidth,height:btnHeight))
btnJoinChannel.setTitle("Join Channel", for: .normal)
btnJoinChannel.setTitleColor(.white, for: .normal)
btnJoinChannel.backgroundColor = .clear
btnJoinChannel.addTarget(self, action: #selector(JRegionCell.loadRegion), for: .touchUpInside)
self.contentView.addSubview(btnJoinChannel)
Buttons never work on UITableViewCells by default because some kind of touch gesture value overrides the new button.
What do I configure to prevent this override? I would like users to touch buttons inside UITableViewCells. Basically, my cells need to behave like UIViews.
Your button is bigger than content view. Buttons don't work if they are out of superview.
I would like to make my custom button to center in tableView Cell, because It feels like the button somehow offset to the left (I think it's because of the frame for accessory type.
I have tried using the didselectrowatindexpath but I feel like I want to do it with UIButton. Do you have any idea how to make my button in tableviewcell center? I have tried using self.view.center but it doesn't work. here's the code:
self.buatAkunBtn = UIButton(frame: CGRectInset(self.buatAkunBtnCell.contentView.bounds, 0, 0))
self.buatAkunBtn.setTitle("Masuk", forState: UIControlState.Normal)
self.buatAkunBtn.setTitleColor(acehBusColor, forState: .Normal)
self.buatAkunBtn.addTarget(self, action: Selector("btncreateAccTapped:"), forControlEvents: .TouchUpInside)
self.buatAkunBtnCell.addSubview(self.buatAkunBtn)
Here's the image from which caused by the code above:
METHOD 0:
If you absolutely want to use code. Here is how I would solve your case:
self.buatAkunBtn = UIButton(frame: CGRectInset(self.buatAkunBtnCell.contentView.frame.offsetBy(dx: 30, dy: 0), 0, 0))
self.buatAkunBtn.setTitle("Masuk", forState: UIControlState.Normal)
self.buatAkunBtn.setTitleColor(acehBusColor, forState: .Normal)
self.buatAkunBtn.addTarget(self, action: Selector("btncreateAccTapped:"), forControlEvents: .TouchUpInside)
self.buatAkunBtnCell.addSubview(self.buatAkunBtn)
Or you can use the Interface builder which would be much simpler and elegant.
METHOD 1:
1) Click on your UIButton.
2) Add constraints relative to Cell 0 to left, 0 to right.
3) Your UIButton is now centered.
METHOD 2 (recommended):
1) Click on your UIButton.
2) Ctrl drag from your button to your cell.
3) Select "center horizontally in container".
METHOD 3 (recommended):
1) Click on your UIButton.
2) Click on the alignment constraint icon at the bottom right.
3) Click on the checkbox: "center horizontally in container"