Swift, button part is outside view, and i wont handle events - ios

So i have a layout in swift:
basically the greyed out part of the selected button wont handle events, i tried:
bottomView.bringSubview(toFront: startButton)
But its not working, im new to swift, but im thinking its because
the greyed out part is outside de UIView and thats why it can't handle the event. Any suggestions or workarounds?

Is the button over the grey area view(kind of Z index)? You can always verify if the button is above the view in storyboard's Document outline (that is on left).Below is a sample view in a view controller. Look at how rabbitButton is over the snailButton

There are a few possibilities here :
Make sure that your gray view is at the top of the view stack. To do that, identify your parent view and : parentView.bringSubview(toFront: grayStartView) just as you did. So the only thing you need to make sure in your case is that bottomView is indeed the parent view of grayStartView.
If grayStartView is a UIView or a UIImageView and not a UIButton, then you need to make sure that it allows user touches, by using : grayStartView.isUserInteractionEnabled = true.
Now it might sound trivial but just in case make sure that you add the target properly if its a button : grayStartButton.addTarget(self, action: #selector(yourMethod), for: .touchUpInside) or a view : grayStartView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(yourMethod))).
Also you need to make sure that, if you add any subview to your grayStartView, or your grayStartButton, you need to call subviewOfGrayButton.isUserInteractionEnabled = false otherwise the subview would then absorb the touches.
Finally I would check that your method (especially the sender) is properly written :
#IBAction func yourMethod(sender: UIButton) {
// ... Do some cool things here
}
If it's still doesn't work, well it would be helpful to learn more about your view hierarchy :)
Note: From what I can see it seems like you're missing a width constraint here (or maybe is it intentional?) so even though it's not related to your problem you may want to check this as well

Related

UIbutton click issue - Swift

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.

Customisable tableview cell swipe to stop on full expansion?

Is there any way to easily achieve the results in the pictures below (maybe by using MGSwipeTableCell or SwipeCellKit or any other pod you know of). I'm pretty sure you can't do that using the default iOS UIKit.
There are 2 things here to consider:
1) The table view cell swipe actions, should be completely customisable (putting an UIView there, so you can put anything you want inside the UIView) or at least, get closer to the screenshot, using 1 and then 5 buttons (therefore, different swipe-actions for smallswipe and fullswipe) ?
2) The tableview cell swipe action, must first (small-swipe) only show the "alarm' icon like in the first picture, and if you click on it, or swipe even further it should look like in the second picture, and most importantly, stay like that, no auto-unswiping (no action to be done, except waiting for input from user, to either select another cell, or any of the buttons there).
I'm not asking for a complete solution, I just want to know if it's possible and relatively easy to do, so I won't waste weeks in order to try to achieve this and find out in the end that it's not possible.
SmallSwipe
FullSwipeOrClick
I think in you tableCell, you can add view to contentView, that will be fill all cell and drag it via UIPanGestureRecognizer. Your action views will be under scrollable view. Something like this:
class SwipableTableViewCell: UITableViewCell {
#IBOutlet var actionView: UIView // under mainView
#IBOutlet var mainView: UIView // take whole place of contentView
#IBOutlet var panGesture: UIPanGestureRecognizer
#IBAction func panAction(_ sender: UIPanGestureRecognizer) {
// drag main view left or right and logic of auto swiping
}
}
I really don't know if you can make a small and full swipe native. The method for swipe for iOS 11 is tableView(_:trailingSwipeActionsConfigurationForRowAt:).
From there, I know there is performFirsActionWithFullSwipe, that doesn't allow full swipe on the first time. I think you can play with this two. In configuration you can add UIContextualAction that has an action, image and even background color that you can set.
I hope it helps.

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.

Replace views in detail part of the screen after user's action

I am new to Swift and app development. I have a design question. I am trying to make a view that contains a slider, but that as soon as the "touch up inside" action is performed, is replaced by a progress bar + button. If the button is pressed, then we go back to showing only the slider. This view will be not take the whole screen, only part of it.
What would be the best way of doing this? I have already investigated several options:
1. using a navigation controller with a segue triggered by the slider that goes into a new scene with a progress bar & button.
2. creating a custom view with two properties: a slider and a custom view (progress bar & button). The slider can be laid out using interface builder, and the custom view can be loaded from a nib file when needed.
3. creating a custom view with two properties: a slider and a custom view (progress bar & button). The new progress bar and button are created programmatically whenever the action is triggered on the slider.
I have already tried options 1 and 2 to some extent with no success. Since I am a beginner, I am trying to use the IB as much as possible. What is the best option (if any) from the list?
You can do this directly on the Storyboard without needing to create a custom view class, but you'll need a few lines of code in any case. Just drag a Slider into your View, and then drag a button and a progress view directly on top of that. Now select the button, and in the Attributes inspector, tick the box next to "Hidden". Do the same with the progress bar. Then just open the assistant editor and connect references to all 3 of those. You'll also need to create an action for the button (I've called it change), and make sure you leave the type field as AnyObject. Add the following line inside ViewDidLoad:
slider.addTarget(self, action: Selector("change:"), forControlEvents: .TouchUpInside)
This line just makes it so that change gets called anytime the user uses the slider. Obviously change slider to whatever you name your UISlider. You can implement the change function like this:
#IBAction func change(sender: AnyObject) {
slider.hidden = !slider.hidden
button.hidden = !button.hidden
progressBar.hidden = !progressBar.hidden
}
This is a simple implementation that just toggles between true and false for each of the items, but you'll probably want to do it differently depending on what this project does.
Now, if you want to put this functionality in multiple places in your app it might be easiest to create a custom view using the same concept as above, in which case check out this tutorial on how to create an IBDesignable UIView.

Bring UIButton to front layer

I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.
Thanks,
I am late for this question, but not too late to answer it by Swift 3
Swift 3
view.bringSubview(toFront: theButton)
A UIButton is a UIView, so you can call -bringSubviewToFront: on your button instance.
uiview's are displayed in the order they are stacked. One can change this to say were to insert the new uiview, which in your case is a uibutton.
One way of doing that is by creating a IBOUtlet of the uibutton and adding that button instance as a subview/newview above an existing uiview.
CODE:
[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];
Also if you are not able to figure out the order of the uiview , then you can check that in xcode
When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)
After that you will able to see all the views rendering on the screen at current instance, using this feature you will be able to understand were is your new uiview in the uiview stack order(shown below).
Swift
The following works great if the view you are working with is being hidden views that are in the same view.
superview?.bringSubviewToFront(self)
Swift 5
In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.
self.view.insertSubview(self.mapView!, at: 0)
i have the same problem with you and a friend of mine helped me
i can bring my button to another subview using this function on your view controller where you declare your button.
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.addSubview(chatButton)
}
button.superview?.bringSubviewToFront(button)

Resources