part of UIButton is not responsive to touch events - ios

I have created a UIView with a subview of type UIButton. The button's size is bigger than the view's. Whenever I touch the button only the part that is the same size as superview is responsive to touch events. I created a drawing of view hierarchy to show you what I mean:
Only the red part of a button is responsive to touch events. Wht is this happening and how can I fix this?
This is the code in viewDidLoad, where I create the button programatically
let button = UIButton(frame: CGRectMake(-8, -8, 52, 52))
moveView.addSubview(button)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: "gogo", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
The UIView is created in storyboard.

Please follow the instruction as I tried your coding
let button = UIButton(frame: CGRectMake(0, 0, giveValueLessthanMoveViewWidthSize, giveValueLessthanMoveViewHeightSize)) //Whatever give value less than your view size(x,y,width,height)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.backgroundColor = UIColor.blueColor()
button.addTarget(self, action: "gogo:", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
moveView.addSubview(button)
Then button action method
func gogo(sender:UIButton!)
{
println("Button Clicked")
}

Related

why can't I tap uibutton in simulator but I can on my hardware iPhone?

I have two buttons on the top of my view controller, which is modally presented. The strange thing is that the buttons are not at all selectable when I click on them in the simulator.
The buttons cause no issues when I use them on my iPhone.
btn.addTarget(self, action: #selector(sendTapped), for: .touchUpInside)
That's the way I add my function to the button
Whole button code:
let post: UIButton = {
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
btn.setTitleColor(UIColor.lightGray.adjust(by: 20), for: .normal)
btn.isEnabled = false
btn.setTitle("Post", for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
btn.addTarget(self, action: #selector(sendTapped), for: .touchUpInside)
return btn
}()
Looks like what I had to do was change the iPhone to an X from an 8 in the simulator to solve my problem.
Possible reasons:
view controller overrides touchesBegan(_:with:) method which don't call the super.
UIView userInteractionEnabled is set to false on that button via InterfaceBuilder or programmatically
button is below a view which has userInteractionEnabled set to false

Change Color of Programmatically Created Button when Pressed

I'm using a function to create multiple buttons for my game.
func createButton() {
let button = UIButton()
button.setTitle("", for: .normal)
button.frame = CGRect(x:15, y: 50, width: 200, height:100)
button.backgroundColor = UIColor.red
self.view.addSubview(button)
button.addTarget(self, action: Selector(("buttonPressed:")), for:
.touchUpInside)
}
I call this function once for testing in viewDidLoad function, but I don't know what code I should put into my buttonPressed() function for the color of my button to change? I tried doing
self.backgroundColor = UIColor.blue
but that didn't work. I also tried using UIButton and button instead of self, but both of those didn't work either. What should I do?
Your code isn't clean Swift 4 code. Here's how to do this:
Create your button like you are, but change Selector to #selector:
func createButton() {
let button = UIButton()
button.setTitle("", for: .normal)
button.frame = CGRect(x:15, y: 50, width: 200, height:100)
button.backgroundColor = UIColor.red
self.view.addSubview(button)
button.addTarget(self, action: #selector((buttonPressed)), for: .touchUpInside)
}
Use the sender that is automatically added:
#objc func buttonPressed(sender: UIButton) {
sender.backgroundColor = UIColor.blue
}
Additionally may I offer a few suggestions?
Check the background color before changing it. No sense in needlessly changing a button that is already blue.
Since you aren't setting the title to your button, set the tag property (you can even add this as a parameter to createButton). This way you can know which button was tapped.
Just make the button an instance property.
let changingButton = UIButton()
func createButton() {
changingButton.backgroundColor = UIColor.red
changingButton.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
}
#objc func buttonPressed() {
changingButton.backgroundColor = UIColor.blue
}

Changing the color of the button when touch in Swift

I have a simple requirement, I want to have color change effect on UIButton when a user touch on button, I have gone through many links but what they suggest, some times work as I touch, I touch hard then it works, for normal touch it does nor work.
I have gone through this link.
How to change the background color of a UIButton while it's highlighted?
From what you've described it should be easy. see the code below.
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
button.setTitle("Click me", for: .normal)
button.backgroundColor = UIColor.red
button.tintColor = UIColor.white
button.addTarget(self, action: Selector("handleTap"), for: .touchUpOutside)
view.addSubview(button)
func handleTap(sender: UIButton) {
if sender.backgroundColor == UIColor.red {
sender.backgroundColor = UIColor.blue
} else {
sender.backgroundColor = UIColor.red
}
}
The above code depends on how you are implementing your UI and where the code is. if you can prvoide more information on your implementation I can update this and make it more specific to your case.

Xcode swift remove button

I add a button like this:
var button: UIButton!
button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(screenWidth/2-50, 40, 100, 50)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
And then I want to remove it with this code:
self.button.removeFromSuperview()
But it doesn't disappear. Why doesn't it disappears?
If you connect the button to your code by creating an IBOutlet, you can remove it from the view with:
button.hidden = true

Make the navigationbar title clickable swift

I would like to make the navigationbar title to be clickable. When user click on it, it should perform a segue. But I have no idea how to do this.
I have tried the following to get the title and apply the Tap gesture to it.
var subviews = self.navigationController?.navigationBar.subviews
if let subviews = subviews {
// Better check for array length before accessing to the 1st element
var subview = subviews [0]
}
but its giving me error Variable subview inferred to have type AvyObject, which may be unexpected
One more approach to add button as a title of navigation controller.
You need to set navigation item title view to your button object
Create button object in viewDidLoad() method:
Swift 4.0 Edit
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = .red
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(clickOnButton), for: .touchUpInside)
navigationItem.titleView = button
Here you can see in last line button is directly set to the titleView of navigationItem which will add button at the center of navigation bar.
Action method for button is below:
#objc func clickOnButton() {
}
Kampai's answer updated for Swift 3:
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside)
self.navigationItem.titleView = button
To get rid of that error, specify a type for your if let constant. I'd also recommend changing that if let constant name since it's the same as the one already declared in the previous line.
var subviews = self.navigationController?.navigationBar.subviews
if let subviewArray:NSArray = subviews {
// Better check for array length before accessing to the 1st element
var subview:UILabel = subviewArray[0] // <-- If the subview's a UILabel
}

Resources