UIButton Programmatically get Default Highlighted Style - ios

I searched for this problem for a while and couldn't find anything about it, but eventually figured out my mistake, so I figured I'd post it here to help anyone with the same situation.
When creating UIButtons in Interface Builder, it automatically styles them for the highlight state (If you press and hold the button, it looks dim/lighter in whatever color you set it as); however, I was trying to create a button programmatically (just a normal text button) and wasn't achieving this result. If I pressed and held the button, the appearance was unchanged, although the button still worked.

The issue was that I was initializing the button with
UIButton *myButton = [[UIButton alloc]init];
instead of
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeSystem];
Which is apparently the best-practice way of initializing a UIButton.

Related

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.

iOS make button that looks like UINavigationItem

Aside from duplicating the look of UINavigationItem into a custom image file, is there any way to add a button to iOS application that looks like a UINavigationItem?
I have UITableView and would like to have buttons on the right side of each cell for specific actions (1 button per cell, but not all cells have the same buttons). So I was looking to base this on the behaviour of "Delete" button that shows up when doing swipe-to-delete on table cells, except I would also need the ability to change the color and text of such a button.
What's the best way to do something like this?
Edit: I know I can just duplicate that look in Photoshop and use an image, but that way to change title and colour I have to go back to editing the image every time. Is there any way to make a button that looks like UINavigationItem programmatically, outside of a UINavigationBar?
The button style I was looking for is UISegmentedControl. It behaves a little bit different than a standard button, but looks a whole lot better. A good alternative to the ugly inflexible UIButton if you don't have hours to spend making buttons in a graphics editor.
UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"My Button"]];
btn.segmentedControlStyle = UISegmentedControlStyleBar;
btn.momentary = YES;
btn.tintColor = [UIColor greenColor];
[btn addTarget:self action:#selector(btn_action:) forControlEvents:UIControlEventValueChanged];

uiappearance for UIButton also changes UIBarButton image?

I am using this piece of code
[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
To change the appearance of UIButtons. This works nicely and does not affect UIBarButtonItems except for one of them (The only one that is not an auto generated "back" button) and only initially. When you touch the barbutton, it reverts back to not having a background image.
The problematic button is a UIBarButton and not an UIButton (Unless it is both, in which case the other UIBarButtons should be affected too).
A clue can be that the other UIBarButton that are not affected are all generated and not created by me in the storyboard.
This must be a bug in the api?
Solved using setTintColor. Was an api bug that since then probably has been fixed

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