How to set button selection and unselection image? - image

when i am touch the button to show selected image and again touch that button unselection image shown.
I am try this coding:
UIButton *btn=(UIButton*) sender;
if (btn.selected=YES)
{
[btn setImage: [UIImage imageNamed:#"reportsel.png"] forState:UIControlStateSelected];
}else{
[btn setImage:[UIImage imageNamed:#"reports.png"] forState:UIControlStateNormal];
}
Anytime click this button not went to else condition.

For Highlighted state, use:
[btnObj setImage:[UIImage imageNamed:#"imgName.png"] forState:UIControlStateHighlighted];
In Normal State, use:
[btnObj setBackgroundImage:[UIImage imageNamed:#"imgName1.png"] forState:UIControlStateNormal];

Related

Change tint color of UITextFields clear Button

i tried to change the color of uitextfields clear button like:
for (UIView *subView in self.urlTextField.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subView;
[button setImage:[[button imageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
}
}
I'm able to add a custom image this way, but the tint color won't change.
As i could´nt find a solution to change the tint color of the original picture, i´ve implemented this workaround
-(void)setClearButtonColor: (UIColor *) color forTextfield: (UITextField *) textField{
UIButton *clearButton = [textField valueForKey:#"_clearButton"];
if (clearButton) {
UIImage *image = [[UIImage imageNamed:#"textfieldClearButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[clearButton setImage:image forState:UIControlStateNormal];
[clearButton setTintColor: color];
}
}
i have created this clear button image (57x57)(#3x)
feel free to use it.

putting buttons on masterdetail template xcode

My question is, what is the most appropriate way to put my own buttons on the upper bar of the Master view in a MasterDetail template.
I would like to put my own buttons on the Top portion of the Master view in the MasterDetail template. It arrives with an edit button on the left and an add (+) button on the right. I commented out the Edit button in the .m file. I drug in my own button in its place. The first thing I noticed is that my button looks different then the + button on the right. It seems to be of uniform size. The next thing is that when I created my button, it did not have a place to specify the Event (touch up inside). Could I have somehow modified the self.editButtonItem. Surely this is a common thing to do - to replace their buttons w/ ours. thaks
Create two custom buttons and set on the navigation item..
// Set navigation left button
UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLeft setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnLeft addTarget:self action:#selector(skipImage:) forControlEvents:UIControlEventTouchUpInside];
[btnLeft setFrame:CGRectMake(0, 0, 60, 29)];
[btnLeft setTitle:#"Skip" forState:UIControlStateNormal];
[btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLeft .titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
//
// Set navigation right button
btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRight setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnRight addTarget:self action:#selector(nextImage:) forControlEvents:UIControlEventTouchUpInside];
[btnRight setFrame:CGRectMake(0, 0, 60, 29)];
[btnRight setTitle:#"Next" forState:UIControlStateNormal];
[btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnRight.titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
[self.navigationItem setRightBarButtonItem:rightBarButton];
//
This example should solve your problem: http://rogchap.com/2011/06/21/custom-navigation-bar-background-and-custom-buttons/ .. originally answered here - > Custom Navigation Bar Buttons..

How to update a bar button item in a toolbar

I have a single playback controller in my app on the navigation bar and is easily found in its implementation file. So I've tried to add a toolbar and move the playback controls to that toolbar, but I don't know how to reference it programmatically and therefore cannot update the image used to indicate if music can be paused or played.
You can do this a couple of ways, one is by setting the image of the UIBarButtonItem not to be confused with the backgroundImage of a button. You can add a bar button like so:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"camera.png"] style:UIBarButtonItemStylePlain target:self action: #selector(pressButton1:)];
The image should be a grayscale image somewhere around 30 x 30 in size much like a tab bar icon. Declare the bar button in the interface file and you can set the image like this:
[barButton1 setImage:[UIImage imageNamed:#"Chats.png"]];
The other way you can do this is by using a custom UIButton like so:
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, 64, 30);
[button1 setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState: UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"Chats.png"] forState: UIControlStateSelected];
[button1 setTitle:#"Camera" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button1 addTarget:self action:#selector(pressButton1:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
Declare button1 in the interface file and then you can change the image like so:
[button1 setSelected:TRUE];

UIButton event is not firing, even if I connect it through xib

I am creating a calculator game, in which I have to popup a UIAlertView, I customized the UIAlertView and put a UITableView programatically into it, When the user tap any of its cell I show a UIView on the top of UITableView, this view has a button, when I clicked that button the event does not fires, I have also installed it manually, but it doesn't work, previously the attachment of event with button is generating an error, for that I uninstalled the event in dealloc, and fix the problem, but after it still the event is not firing.
i was have case such your and i have to add it pragmatically
in .h declare
UIButton *btn;
in .m add it to the UIView
UIView *TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
if (btn) {
[btn removeFromSuperview];
// [btn release]; // for not using arc
}
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 10, 25, 25)];
[btn addTarget:self action:#selector(ClickMe:) forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:btn];
[self.view addSubview:TempView];
and the action is
-(void)ClickMe:(id)sender{
//here is an action
}
it's working for me

uibutton font subclass

I have the following method in my UIButton class:
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.textColor = [UIColor greenColor];
}
return self;
}
Works great except I can't see the border around the button: any ideas why? What do I have to do extra to make the button border show?
Thank you
Do not subclass button do everything in IB it would be more better. This is how You can achieve what you want:
Select button -> go to Attributes Inspector -> Type -> select Rounded Rect. That will give You default button look.
Then select font (Press on T Letter) like this:
Also select text color behind Font.
Result:
EDIT:
Create button programmatically:
//create the button with RoundedRect type
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 150, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
//set the button's font
button.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
//set the button's line break mode
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
//set the button's text color
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//button.titleLabel.textColor = [UIColor greenColor]; // don't use this because button's text color after clicking it will be blue (default).
//add the button to the view
[self.view addSubview:button];
Result:
Note: Do not use textColor because Your text color after clicking the button will be default blue.

Resources