How can I load a jpg image into the UIButton.
I want one image for when it is Not pressed.
And another image for when it is pressed.
thanks
(looking to do this programmatically... no interface builder)
[myButton setBackgroundImage:[UIImage imageNamed:#"not_pressed.jpg"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"pressed.jpg"] forState:UIControlStateHighlighted];
not_pressed.jpg and pressed.jpg are needed to be present in resource. Also you can use setImage:forState:. Please check UIButton reference for details of these methods.
You can see in interface builder there are properties and you can see that there are options for backgroundImage and Image for each state(normal,highlighted,selected,disabled) go to interface builder properties of UIButton you will find there. For images to appear in the combo box you need to add images to your project.
UPDATE
Code Snippet
UIButton * btnShare=[[UIButton alloc] initWithFrame:CGRectMake(0,0,50,50)];
[btnShare setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateHighlighted];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateSelected];
[btnShare addTarget: self action: #selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
Related
I want to add a borderless UIButton to my view. Using the interface builder, I do this by dragging a Round Rect Button from the object library. Then, on the attributes inspector, change the Type to Custom and leave the title to "Button." Everything is okay with the interface builder. But, this is not the case with the programmatic approach. Programmatically, this is how I do it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
When I run the app, the button is not displayed. Maybe I'm missing something here but when I change the type to UIButtonTypeRoundRect the button is displayed. But, again, I want the button to be borderless.
Anyway, I can always use the interface builder. However, I want to understand why the programmatic approach doesn't work. So, does anyone know what the problem is?
UIButtonTypeCustom is really, actually a custom button type. There are no meaningful values setup for it by default. If you want to display it, you have to set its background color and/or its title color to a non-transparent one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Here i have used your code the Thing you missing is that
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Hi I was in the same situation.
I do not know why somewhere in my code there was
button.translatesAutoresizingMaskIntoConstraints = NO;
Once I've deleted that line I was able to set my button frame without any issues.
Hope this helps
i stared to built an app and i want that all my buttons in my app will look the same.
i write code for one button and i want my other buttons look alike.
here is my button code:
UIImage *butimage = [UIImage imageNamed:#"button1.png"];
UIImage *butimage3 = [UIImage imageNamed:#"button1press.png"];
UIImage *butpress = [butimage3 stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *butimage2 = [butimage stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button1 setBackgroundImage:butimage2 forState:UIControlStateNormal];
[button1 setBackgroundImage:butpress forState:UIControlStateHighlighted];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
how can i do it?
thanks
Try this:
[button1 addTarget:self action:#selector(anAction:) forControlEvents:UIControlEventTouchUpInside]
I'm assuming you've created only a button, and assigning it all the images.
If you create more buttons, the code for giving them an action is the above one.
For the anAction: part, create a -(void)anAction:(id)sender (i.e) in order to tell the button to run that specific action.
EDIT
Declare your buttons as IBOutlets, place them in your viewController.xib and link them.
Then declare their properties in viewWillAppear or viewDidLoad (the code you wrote, basically).
Then if you want them to perform some actions, write some -(IBAction)actionForButton:(id)sender and just assign them to the buttons in IB.
I'm customizing UINavigationBar and have to set a custom background image to the navigation buttons. This is done this way:
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([[[view class] description] isEqualToString:#"UINavigationButton"]) {
UINavigationButton *button = {(UINavigationButton *)view};
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
}
}
Won't this be rejected by Apple?
How can I make UIButton/UIView corners to be rounded?
A UIView object has a property named layer (CALayer) and a layer has a cornerRadius property.
Create your custom buttons as normal UIButtons, with your custom image. Then create UIBarButtonItems with your new UIButton as a custom view (you can see how to do this in the UIBarButtonItem class reference).
Then, set your view controller's navigationItem to have your new UIBarButtonItems as the leftButtonItem, rightButtonItem and so on.
Instead of this do like this.Take a UIButton and then add it to the navigation buttons.
The following code basically does work, only the titleLabel is not shown when the result is displayed.
buttonRectis just a CGRect containing the buttons frame. I don't think the cause of the problem is there, because the button is displayed at the position he should be at.
[contactButton.titleLabel center] did not help, bringing the subview titleLabel to front didn't either.
UIButton *contactButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
contactButton.frame = buttonRect;
contactButton.titleLabel.text = #"Contact Someone!";
[contactButton addTarget:self action:#selector(showContactWindow) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarController.view addSubview:contactButton];
I really have no idea what could be causing this.
[myButton setTitle:#"Contact Someone!" forState:UIControlStateNormal];
I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.