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
Related
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.
Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. If it is a duplicate and it's been answered by all means show me the way!
But my question doesn't deal so much with a button changing an image, more so the button is the image.
So my button is an image and when pressed a sound plays and then the image changes until the sound is done. My problem is I can't get the image to change.
Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done:
UIImage *dolDerp = [UIImage imageNamed:#"dolphin2.png"];
[imageview setImage:dolDerp];
I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. I also would prefer to just have the button objects image change if possible?
If anyone could offer some help it's much appreciated!
the best thing is in Interface Builder assign the two images to the button Normal/Selected states.
in code just use this line:
myButton.selected = YES;
//or
myButton.selected = NO;
What #Mahmoud Fayez suggested is good. In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage
If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code.
In a program much like the who want to be a millionaire... I have 4 option buttons. It should flash red or green on press. The four buttons have either red or green images as background images. In the code setup I have such a method ready to setup the first question and when the second question gets set, the background images change accordingly.
Currently the default blue highlight is always appearing on the first pressing of any one the buttons and after that there are other issues.
I could paste some different approaches here, (but they dont work). So any suggestions would be good.
Also in the IB, when you set the highlight with background image, that works. I would use use, but for the same button, the back ground image must be changed programmatically.
Thanks in advance for the help/suggestions!
Use two State UIControlStateNormal and UIControlStateHighlighted
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1-Highlighted.png"] forState:UIControlStateHighlighted];
I have to implement a toggle like behavior between to differentiate between two states, client doesn't want UIbuttons because he has his own png's and wants them to be used.
so I need to have to UIImageViews next to each other.
I am talking about UIImageviews because the toggle behavior the client wants should be a sliding one, as in the user has two options in the control ( like on / off ) he slides the UI from on to off, or off to on, I was given 4 png's: 2 for the on state ( normal and depressed ) and 2 for the off state ( Normal and depressed ) .
I also looked into UISwitch, but again to customize it the way I want to seemed cumbersome, any ideas?
You can achieve what your client wants this way:
UIButton *nameOfButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nameOfButton setImage:[UIImage imageNamed:nameOfYourClientsPNGFiles] forState:UIControlStateNormal];
[nameOfButton addTarget:self action:#selector(actionForButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nameOfButton];
//Note that all the words are to be replaced with what your methods and pointers are.
Also if your clients PNG files are not the traditional square images I strongly recommend looking into this Custom Class:-)
OBShapedButton
It sorts out the problem that the user can only tab and click the shape of the image.
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];