UIbutton click issue - Swift - ios

I'm trying to add login button overlap on subview, the touch event for uibutton does not work on lower-half.
Can anyone provide me an idea to achieve design like this, with touch event work on all corner ?

You view set up should look something like:
As Inderjeet mentioned in his comment, "You have to keep button outside the UIView otherwise it is not tap-able. Maintain the position of button according to UIView by using constraints."
Now to answer why, set clip to bounds property of your view to true. You will see, on run time, portion which is not tap-able, is also not visible.

Related

How can I properly handle touch events of a UIButton on top of another UIButton?

I'm trying to recreate the function of the clear button in UITextFields but with a UIButton and as such have a UIButton as a subview of another UIButton. I read somewhere that I need to have the superview handle the touch events of the subview.
The post that hinted at that was outdated and in obj-C so I'm looking for modern and swift version.
Placing a button on another button is a kind of bad implementation in spite of any requirements.
Hope you find out a right way to solve this problem.
Or you can just use a combination of enabling or disabling buttons according to the situation.

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

iOS: How to disable Fading in and out effect of UIButton

First of all, sorry for the title and asking this incredibly question but I simply couldn't figure it out. Also, since it is not related to code, I don't have code to show
I am working on an app and using iOS7 and I created a button from IB, set its background image to an image I designed. Connected it with header and set its touch up inside action as an IBAction
Yet, here is my problem. Whenever I click on the button, as an effect the image fades half transparent. I do not want this default attribute. I checked all the states on IB (highlighted, disabled, selected) and couldn't figure it out.
If I create the same button programmatically, only the text color changes, however when I set the background image, image fades (perhaps to indicate the button being pressed). How can I remove this effect?
A bit late but I believe this would solve the problem
in the xib file, set the button's type to Custom.
My button was set to system and when pressed on, it shows transparency and has a little fade in effect. Once I changed it to custom this effect is gone.
This cannot be changed once button is created. only in xib or when the button is first created.
have you tried this one yourButton.adjustsImageWhenHighlighted = NO?
If you set same background-image/background-color for every buttons states in IB (highlight, disable, selected) you will not get any fading in UIBUtton when you press it. You can set text color for every states also. If needed you can set different colors for every state and check the press action.
Hope the answered you expect. Thanks
Storyboard Solution
1:set button type to custom
2:uncheck "Highlighted adjusts Image"
If you want fading in & out effect your UIButton Type Should be a system, not custom.

How to detect "Drag Enter" on UIScrollView

Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events.
My question: is there some way to achieve the same for a UIScrollView?
I.e., I tap somewhere outside the scrollview, drag my finger onto the scrollview and as soon as I enter the scrollviews frame it starts panning? (Because by default it doesn't, I have to start my touch INSIDE the scrollview in order to pan)
If you want to do this, you will have to do a custom implementation using -touchesBegan, -touchesMoved, and -touchesEnded
The documentation for the UIResponder class (which all ViewControllers inherit from) that allows you to do this is here.
Hopefully you can figure out what to do from here. :)
As an extra hint, you will also most likely need to use this function
bool contains = CGRectContainsPoint(CGRect rect, CGPoint point);
Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events
Actually, no you can't. A UIControl will not get any of the control events unless the touch started in the control.
And that's for the same reason that you are seeing a similar effect with the scroll view. This is the entire basis of touch delivery on iOS. A touch "belongs" to the view that was initially touched.
After all, the runtime cannot possibly know that you are going to drag into the scroll view...

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources