Center UIButton programmatically - objective-c

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

Related

create hard custom button

I can not figure out how to construct this button. I need help in the creation of this, note that the strip is not painted when the button is active. What elements should I use?
on link you can look this button
http://i.stack.imgur.com/0mmJ0.png
UIButton *clone = [[UIButton alloc] initWithFrame:frame];
[clone setTitle:strDate forState:UIControlStateNormal];
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
clone.frame = CGRectMake(19, post_offer_top*(i-1)+22, 281, 75);
clone.tag = my_id;
// [clone setTitle:[[buttons objectAtIndex:i-1] objectForKey:#"title"] forState:UIControlStateNormal];
clone.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
clone.contentEdgeInsets = UIEdgeInsetsMake(44, 31, 0, 0);
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clone setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[clone.titleLabel setFont:[UIFont fontWithName:#"PF DinDisplay Pro" size:10]];
[clone.titleLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
any ideas?
You should initialize buttons like this [UIButton buttonWithType:UIButtonTypeCustom]

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]];

UIImage with resizableImage option

I want to set image as a background on my button. So i create an image:
then I want to implement it as stretchable:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];
[button setTitle:#"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [_model.background resizableImageWithCapInsets:UIEdgeInsetsMake(6, 8, 6, 7)]; //this is screen 1
//UIImage *buttonImage = _model.background; //this is screen 2
[button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self addSubview:button];
I want to background stretch like in screen 2 but corners will sharp like screen 1. _model.background is UIImage contains original button background from the top of this post.
Am I using it right? I follow the documentation so UIEdgeInsetsMake(top,left,bottom,right) should be unstreachable corners, right?
According to the docs, the part inside the insets (the part not locked via cap settings) is tiled. So you should have a small 1 pixel region to tile. E.g. make the insets be UIEdgeIndetsMake(20,8,19,7)
This produces:
honestly, this is not the best picture for this effect but, please, try this line:
[button setBackgroundImage:[[UIImage imageNamed:#"image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.f, 62.f, 20.f, 62.f)] forState:UIControlStateNormal];

UIButton to show BackgroundImage, Image and Title

I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.

Changing UIBarButtonItem colors

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];