iOS - Replace a label by a button - ios

I've added a label in a view controller via the interface builder, and I've added a lot of constraints to it, but I want to replace it by a button. Can I accomplish this without losing all the constraints ? Thanks a lot in advance

I'm not sure if you can save your constraints inside interface builder. However, you can add a tap gesture recognizer to make the label perform an action when it is tapped (act like a button).
This code can help you get started:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)
func handleTap(_ sender: UITapGestureRecognizer) {
// perform some action when the label is tapped
}
You can look at this question for more information.

Don’t replace by button
Add tap gesture for click action

you don't need to remove that label. Just addGestureRecognizer a tap gesture on label.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)
func handleTap(_ sender: UITapGestureRecognizer) {
// perform some action when the label is tapped
}

Related

Bringing UIViews to front on tap

https://github.com/raylu135/Views-
I'm working on this project, I'd like to add a function that allows me to bring the tapped view to the front.
After some researches, I tried to add an UITapGestureRecognizer and view.bringSubviewToFront but it didn't work... maybe I'm not doing it the right way, how can I do?
It won't work like in the link.
1, You need to setup the tap gesture recogniser
2, You need to create an #objc function and call this on action.
//paste it into viewDidLoad.
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
tap.cancelsTouchesInView = false
//Uncomment next line, if you want more than one tap to activate gesture.
//tap.numberOfTapsRequired = 5
self.view.addGestureRecognizer(tap)
// Create a function
#objc func tapped(){
self.view.bringSubviewToFront(UIViewWhatYouWantToBringToFront)
}

iOS: TapGestureRecognizer in UIView not fired

I have create an date component and have problems with the GestureRecognizer.
First line is a StackView with labels. If I attach a TapGestureRecognizer it is fired on touch. The following Lines are subviews in a Stackview consisting of a label and an image view. I can add the Recognizer to the subview, to the label or the image view. It never gets fired and I made sure that userInteraction is enabled.
What could be the problem?
Here an example how I add the recognizer:
func addTap(){
dayLabel.isUserInteractionEnabled = true
imageView.isUserInteractionEnabled = true
isUserInteractionEnabled = true
tap1 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap3 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
dayLabel.addGestureRecognizer(tap1!)
imageView.addGestureRecognizer(tap2!)
addGestureRecognizer(tap3!)
}
#objc func handleTap(recognizer: UITapGestureRecognizer){
logger.debug("tap")
changeSelection()
}
You can find a little demo project boiled down to the problem here: https://github.com/ogezue/datedemo
Problem I can see is your view must be cover with Image and label
and you are adding same UITapGestureRecognizer to all so that is added on last object say view (which is covered with image and label) so it may not able to get tap event
You need three different objects of UITapGestureRecognizer you can't add same tap gesture on different views
Hope it may solve your problem
For every view (Label/Imageview) you should create a tap object not the same one added to both . . .

Swift 3 multiple gestureRecorgnizer SWRevealController

I have two gestureRecognizer I want to use.
1) Tap to close side menu (SWRevealController)
2) Tap view to dismiss keyboard
override func viewDidLoad() {
super.viewDidLoad()
menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginController.dismissKeyboard))
self.view.addGestureRecognizer(tap)
}
Having both of these lines together, the dismiss keyboard gestureRecognizer trumps the revealViewController. How can I combine this this use both gestures together?
Make condition in LoginController.dismissKeyboard method.
check if isKeyboard is open, then close it. Otherwise close the (SWRevealController) sidemenu.
For check keyboard is open or not you can use post notification of UIKeyboardWillShowNotification and UIKeyboardWillHideNotification methods.

Add single, double and triple tap gestures to all "buttons" in custom keyboard in swift

I'm trying to create a keyboard the allows single tap, double tap, and triple tap. So I want to add a UITapGestureRecognizer() to each button in my keyboard. I know how to do this manually from the xib file (add each letter it's own gesture, which would take ages) but not quite sure how to do it in the Controller.
I wrote this for double tap in the viewDidLoad() method:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
and created a doubleTapCharacter() method but it's still not working. I also want to be able to send information to the doubleTapCharacter method.
Any help would be much appreciated. Also, I'm very new to swift so if the instructions are complicated, I'd highly appreciate it if you can break it down a little.
create and add the gesture recognizers:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
then implement the target method:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}

How to trigger UIButton target event before parent TapGestureRecognizer

It's a simple question,
I have a UIButton with a target action
addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
func buttonPressed()
{
self.selected = !self.selected
}
This button is the child of a parent UIView
containerView.addSubview(button)
I add a UITapGestureRecognizer to the container
let tapG = UITapGestureRecognizer(target: self, action: "toggleView:")
containerView.addGestureRecognizer(tapG)
in func toggleView(gest:UIGestureRecognizer) I check the status of my button.
The problem is that in toggleView(), the status of the button has not been set... YET!
EDIT
if I do
print(button.selected)
I only get the button value BEFORE the touch event, not the new one.
any idea?
Try to change
func toggleView(gest:UIGestureRecognizer)
to
func toggleView(gest:UITapGestureRecognizer)
Hope it helps!
I don't exactly get what you are trying to accomplish, If you want to perform certain action on button click and another action when tapped outside in containerView then you can do
-(void)toggleView:(UITapGestureRecognizer *) gest{
CGPoint tapLocation = [gest loactionInView:containerView];
if(CGRectContainsPoint(button.frame, tapLocation)){
// Tapped on Button
}
else{
// tapped outside in containerView
}
}
Note: As am not learning swift, i gave snippet in Objective-C. you should be able to translate it into swift easily :)

Resources