Custom badge issue in iOS - ios

I have an view which is having a button and a label.
The label increments whenever the user adds items to the cart, which is done by using custom badge. But what I need to know is how to put onClick for that label. Button takes the onClick but label is not taking an action(UIGuesture).
I have attached the image where in clicking on the cart goes to another view, but clicking on label does not work.
Any help would be appreciated.
thanks.

Try enabling the user interaction on label.
label.userInteractionEnabled = YES;
By default user interaction is disabled on UILabel, which doesn't let any gesture to be recognized while applied on UILabel.
Better to use an open-source implementation:
MKNumberBadgeView
CustomBadge

Related

Why is UIButton.titleLabel?.text always switching back after I changed it in Swift 5 (UIKit)?

I ran into an issue with my UIButton.
After the User presses the button, it activates an IBAction which is supposed to change the titleLabel.text of my UIButton.
self.btnAppleHealthKit.titleLabel?.text = "Already pressed."
Whenever I press the button, the text changes for about a quarter second to "Already pressed", but switches back immediately to the previous text.
Important Note: The whole thing is inside a completion handler but I wrapped it into DispatchQueue.main.async { }
Can you guys think of an idea why this error appears?
Thanks for your help in advance.
It seems, UIButton does not allow to configure text or color of titleLabel.
From apple doc.s
Do not use the label object to set the text color or the shadow color.
Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:)
methods of this class to make those changes. To set the actual text of
the label, use setTitle(_:for:) (button.titleLabel.text does not let
you set the text).

how to create my own radio buttons

I am new to Swift and Xcode so I need help with what I am doing. And what I am doing is this.
I have three buttons one of them starts out checked. and what I want it to do is when I click on another button it checks it and unchecked the previous. I want my own radio buttons
For Radio Buttons there is nothing that comes built in.
You can use SSRadioButtonsController
You can create a controller object and add buttons array to it like
var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])
You can also use something like this tutorial.
The standard Apple class that is designed to work with radio button groups is NSMatrix. Specifically you'd create an NSMatrix with mode "NSRadioModeMatrix". NSMatrix takes an NSCell for its list of items. So you can use any built-in NSCell subclass (like NSButton) or anything else custom you would like.
You can manage manually by setBackgroundImage and setImage property. at initial time set rounded image as setBackgroundImage for all three button. At button click event set "bulletin" image by setImage property for selected button and set nil for all other button setImage property.

Button Responsiveness at UIScrollView

I am dynamically creating a uiscrollview and i place some uiview's that contains some label and buttons inside to display some news. Every button inside the sub uiview calls a rest function to like, unlike or share the news. Some of the buttons opens overlay screens like comment news. I am assigning actions to buttons inside the main form that contains the uiscrollview.
When i click a button that opens an overlay screen. When i close the overlay screen and hit Like button, it does not respond to touches. After attempting one or two more times, it works.
Does anyone has any idea about this issue?
Are you using UITapGestureRecogniser for "Click action"? If yes, you propably want to set flag "cancelsTouchesInView = NO" for this recogniser.
Check to see if there is a clear view covering your button. That view will consume your tap, so the button never sees it.

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.

ios navigation buttons

I'm doing a custom nav in ios for the first time. I have six buttons laid out in a row. When I tap the button, I want the image to change. However, the button is not togglable. The only way a button can be unselected is if another button is touched. Only one button can be active at any given time.
My idea:
use UIButtons
change UIButton image on touch
keep track of the active button inside the navigation class
when an inactive button is touched, make the currently active button inactive and turn the touched button to active
I want the end product to work like a custom TabBarController, but without switching layouts. I just want to edit the content in the current ViewController.
Is there a better way to do this?
You could just use the UISegmentedControl, which has that functionality already. If you need to significantly customise the look and feel though, your UIButton solution sounds fine.
I setup the UIButtons in the Interface Builder like so:
Default with unselected background image
Disabled with selected background image
On touch, a button is self.enabled = NO which makes the UIButtons look change. However, the button goes dim, so I also implemented self. adjustsImageWhenDisabled = NO.
This way a button can't be re-selected once it's "active".

Resources