Changing UIBarButtonItem colors - cocoa-touch

This is a pretty simple question and I really can't find the answer!
So okay, by default if I set the tintColor of a UIToolbar, the UIBarButtonItem's colors will be the same.
I only want to change the colors of my UIBarButtonItems and also change their text's colors.
Is there anybody out there having an idea on how to do so?
I really can't believe it is so complicated, really... changing an UIButton properties is so easy, I can't see why it's not the same for an UIBarButtonItem...
Thanks so much!

Not sure you found the answer. Here's how I do it:
UIImage *buttonImage = [[UIImage imageNamed:#"btnGreen.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(saveDateEdit:) forControlEvents:UIControlEventTouchUpInside];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
doneButton.frame = CGRectMake(0.0, 0.0, 50, 30);
btnDone = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
[doneButton release];

Related

extra button in rightbarbuttonitems not showing

I create two UIBarButtonItem in the code below. Later I add them to rightBarButtonItems yet only one of the two buttons shows up (the one I put first on the list).
I thought perhaps that the title's view (which is not set) would be in front of the rightBarButtonItems, since the Apple docs says "If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed."
But that doesn't seem to be it either, since the view's frame = {0,0}{0,0}
I can't figure out what I'm doing wrong. Can anybody tell me how to get all rightBarButtonItems to show? Code:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareAction:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *wishlistButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[[NSArray alloc] initWithObjects:shareButton, wishlistButton, nil]];
Use UIBarButtonItem:
UIBarButtonItem *button = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"custom-bar-button.png"] style:UIBarButtonItemStylePlain target:self action:#selector(barButtonTapped:)];
instead of:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

How to reduce padding between UIBarButton items in UINavigationBar IOS 7?

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addButton.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(addSubProperty:) forControlEvents:UIControlEventTouchUpInside];
[addButton setFrame:CGRectMake(0, 0,51,44)];
UIBarButtonItem *firstBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setImage:[UIImage imageNamed:#"deleteButton.png"] forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteProperty:) forControlEvents:UIControlEventTouchUpInside];
[deleteButton setFrame:CGRectMake(0, 0,50,44)];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-10];
UIBarButtonItem *secondBarButton = [[UIBarButtonItem alloc]initWithCustomView:deleteButton];
NSArray *navBtnArr = [[NSArray alloc] initWithObjects:negativeSpacer,firstBarButton,secondBarButton, nil];
self.navigationItem.rightBarButtonItems = navBtnArr;
After this i got this result ,as you guys can see i did able to manage move my first barbutton left byt that negativeSpacer but i donot know how to reduce space between those two button.
and by doing this my back also goes disappear any idea will really appreciated .
Using Xcode 6.4, This worked beautifully for me with 1 line of code!
https://stackoverflow.com/a/26469607/3634990
self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25);
Note, this moves the image location, but not the touch area, so it's a great fix only if you want to move the image just a little bit.

NavBar\Toolbar button text color?

I am using the following code to create my toolbar buttons....I would like to change the text color to black or better yet ...set the background image to color the text .....setBackgroundColor would not work....any help is appreciated, Thanks.
UIButton *btnEditLogin = [UIButton buttonWithType:UIButtonTypeCustom];
[btnEditLogin setBackgroundImage:[UIImage imageNamed:#"btnPlain.png"] forState:UIControlStateNormal];
[btnEditLogin setTitle:#"Edit Login" forState:UIControlStateNormal];
[btnEditLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnEditLogin.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0f];
[btnEditLogin.layer setCornerRadius:5.0f];
[btnEditLogin.layer setMasksToBounds:YES];
[btnEditLogin.layer setBorderWidth:1.0f];
[btnEditLogin.layer setBorderColor: [[UIColor brownColor] CGColor]];
btnEditLogin.frame=CGRectMake(0.0, 100.0, 90.0, 30.0);
[btnEditLogin addTarget:self action:#selector(LoginBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *editLoginItem = [[UIBarButtonItem alloc] initWithCustomView:btnEditLogin];
You can just use the titleView attribute of NavigationItem to do what you need to do. You can do the following:
UILabel *myLabel = [... enter all the code you want to customize the text, etc here];
self.navigationcontroller.navigationitem.titleView = myLabel;
And you should be done... unless I'm misunderstanding the question.
Use this code to change toolbar textcolor.
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]];

Image in UIButton is darker tharn original

I'm trying to add a UIButton in a cell which already has a UIImageView in the background.
I put this code in configureCell
UIButton *mail = [[UIButton alloc] initWithFrame:CGRectMake(10, 270, 40, 40)];
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor whiteColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];
It works but the image in the UIButton is seems darker
I tried these but it didn't changed anything
mail.alpha = 1;
[cell.contentView bringSubviewToFront:mail];
Does anyone have an explanation for this, and a solution as well.
I had the same problem once and I solved it by setting the button's image for the UIControlStateHighlighted
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateHighlighted];
Hope it works for you
Try this
UIButton *mail=[UIButton buttonWithType:UIButtonTypeCustom];
mail.frame= CGRectMake(10, 270, 40, 40);
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor clearColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];

Center UIButton programmatically

I have a UIButton which i add programmatically. No problems here, it stetches as it should and looks good.
However, when I tilt the phone to landscape the button gets out of place, and I want it still centered.
Portrait:
Landscape:
I have tried a bunch of thing but cannot seem to get it working, it seems to me that the autoresizing should take care of it, however... it doesn't work in that case.
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 49)];
_notMeButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *butImage = [[UIImage imageNamed:#"notme.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6];
UIImage *butImagePressed = [[UIImage imageNamed:#"KC_notmePressed.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[_notMeButton setBackgroundImage:butImage forState:UIControlStateNormal];
[_notMeButton setBackgroundImage:butImagePressed forState:UIControlStateHighlighted];
[_notMeButton setTitle:NSLocalizedString(#"Change address", #"KlarnaCheckout") forState:UIControlStateNormal];
[_notMeButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[_notMeButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[_notMeButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_notMeButton.titleLabel setShadowColor:[UIColor whiteColor]];
[_notMeButton.titleLabel setShadowOffset:CGSizeMake(0, 1)];
[_notMeButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
[_notMeButton addTarget:self action:#selector(thisIsNotMe) forControlEvents:UIControlEventTouchUpInside];
[_notMeButton setTag:1];
[_notMeButton sizeToFit];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
[_notMeButton setFrame:CGRectMake(10, 10, _notMeButton.frame.size.width+20, _notMeButton.frame.size.height)];
[_notMeButton setCenter:footerView.center];
[footerView addSubview:_notMeButton];
return footerView;
}
Your idea about autoresizing mask is right, but you're setting it incorrectly:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
The second line overwrites what you set in the 1st. Correct way to set several flags will be:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
One thing before I answer your question:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
The second sentence overwrites the first one, you should use the | operator so both resizing mask are applied:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
To keep the element where it is when the view changes its size, you need to tell it not to use any resizing mask:
[_notMeButton setAutoresizingMask:UIViewAutoresizingNone];
And the equivalent in Interface Builder is this:
I hope it helps