CALayer change borderColor on UIControlState change - ios

I have a customized button that has a transparent background and a custom border set in the button's layer.
I want to change the border color of the button to a darker color when the button is pressed, e.g. during UIControlStateSelected, but I can't find an easy way to do this.
Is this possible? Or do I need to find a workaround for this?

You can listen to an event like UIControlEventTouchDown and configure the border when it occurs. You need to add an observer as follows:
[yourButton addTarget:self
action:#selector(configureBorderIn)
forControlEvents:UIControlEventTouchDown];
You should add an observer to configure the border when the button is released, for example detecting UIControlEventTouchUpInside. You have a list of all events related to buttons here: UIControl class reference.
Hope it helps!
have a good day.

Related

Firing a UIButton instantly

Right now I have a square button like so
OFF
ON
I want the button to change instantly when I first press on it (not wait for the finger to be lifted), so I need it to fire instantly and only once because I will also include a long press gesture for a different action. How would I go about making the button behave like this? Right now it only works for when I lift my finger off. (all the config has been done in the interface builder).
EDIT:
As liushuaikobe explained below I have set the event to "Touch Down" now. The button is now firing correctly but the image won't change until I take my finger off:
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
button.selected = YES;
}
The button should be selected instantly, I have the off image set in the interface builder for default and the on one set for selected. Any ideas what I'm doing wrong here?
Add a action for the button on state UIControlEventTouchDown.
This is the state when button is "pressed" rather than the finger left the screen (which is called UIControlEventTouchUpInside).
Connect your button action to your file owner with Touch Down event like this:
if your button connected with IBoutlet then you can do it by this way
[switchButton addTarget:self action:#selector(forgotBtnClick:) forControlEvents:UIControlEventTouchDown];
Set an image for the state highlighted:
[yourButton setImage: yourImage forState: UIControlStateHighlighted].
Set your highlighted image (UIButton's property) as on.png, assuming that default is off.png only if you want the effect not the method to be called, if you need your method to be called on press down then you have to change your control event from UIControlEventTouchUpInside to UIControlEventTouchDown

Label over button, how to highlight text on button press in iOS7?

I have a button and multiple labels placed over the button in storyboard. For the button I specified a default and a highlight state. Also for the labels I specified the highlight color in storyboard.
However on button press the font color of the label does not change to white. Am I missing something? I would like to configure this behavior in storyboard, not programmatically. Is this possible? Or do I have to create a custom button?
The problem is that the UILabel does not get the touch events because it simply does not handle touch events by design, it's just for showing text.
You might want to create a subclass of a UIButton but this is not a good idea since it's kind of a cluster class.
Best way to do it is creating a custom button class by subclassing either UIControl or UIView. With the later you could add it in your storyboard by changing their class to one of your button subclasses. In the subclass make your customizations in the initWithCoder: method.
If you decided to choose the UIControl way of doing this. Look at setHighlighted: method:
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted: highlighted];
// Highlight your labels here
}
Useful link: UIControl Class Reference

How to make a UIButton invisible but also still work if pressed?

I have a situation in which I need to have a button added to my view using Interface Builder, but the button shouldn't be seen, but if the user clicks the location of the button, the button can still call its method.
I've tried setting the opacity to zero, or changing the button to 'hidden', but both of these strategies result in a button that is completely invisible, but also useless, as it can't be pressed.
Does anyone have a solution for this?
You can try to custom type UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
With empty title, empty background image.
Or set the type from the storyboard or Interface Builder:

How to change custom button's text?

I need to set text to the this custom button.I thought it would be as easy as using setText method to set its text. But, unfortunately, it doesn't work.
So, why this approach doesn't work for any custom buttons?
When we changed its type to custom, basically how it effects this UIButton?
Thank you.
EDIT: I've created this UIButton through IB. The code used for changing text is this :
[self.btnDiscover setTitle:#"Discover" forState:UIControlStateNormal];
Use setTitle:forState: instead of setText, e.g.:
[button setTitle:NSLocalizedString(#"CONTACT_BUTTON", #"Contact Us") forState:UIControlStateNormal];
Also, make sure you are using setBackgroundImage:forState: to set the button image and not setImage:forState:.
As to your question about the custom style, assigning UIButtonTypeCustom to a button simply means that the button is assigned no style (source):
UIButtonTypeCustom
No button style.

Round UIButton

I want to know whether drawing a round UIButton(not rounded rect) is possible.
When I add a round image in a UIButton of type custom, then it looks like a round button. But at the moment the button is clicked the boundary of the button becomes visible, so that it looks like a square button, then again when the click ends it looks like a round button.
I want the button to look like a round button even at the moment the click happens. is this possible?
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Result
Just create two rounded button images, one for the pressed state and one for unpressed.
In IB, create a UIButton of type Custom. Go to the section where you set a background image, and set the dropdown with "All" to normal - now set the image to your unpressed image.
Then change the dropdown to "Selected", and put in your pressed image.
Now change the fill type to be aspect fit, and make the button sqare. Use as a normal UIButton anywhere. You can of course also easily do this all programaitcally with similar steps (setting images for UIControlStateNormal and UIControlStateSelected).
You'd probably have to create a custom control for that, but do you really need a round button?
Every platform has its UI conventions, and the iPhone is no exception. Users expect the buttons to be rounded rectangles.
UPDATE in response to comment:
If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.
You can use an UIImageView and its touchesBegan method.
UPDATE 2:
Wait a second. What kind of radio button are we talking about? For some reason I thought you were trying to imitate a real radio. If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker.
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
but.layer.cornerRadius=50;
From what I know of the SDK (admittedly I have very little experience beyond tutorials in books/screencasts), you'd have to create your own control to have a radio button. There are a couple of suggestions on how to implement that in this question.
That said, from a user's perspective I feel like it would be better to stick to the native controls. iPhone users are used to specific controls (i.e. UITableViews or UIPickerViews) for this type of interaction. Straying from practically universal UI conventions tends to make the user experience more jarring.
take the help of this site for creating Round Button on iPhone.
You can get many custom controls :)
http://www.cocoacontrols.com/controls
http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton
H#PPY CODING !
Thanks & regards,
Gautam Tiwari

Resources