您的位置:首页 > 编程语言

代码设置按钮不同状态的颜色

2012-07-10 22:44 447 查看
代码设置背景色::::::
UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
To reomove image: [button setBackgroundImage:nil forState:UIControlStateNormal]
如果通过IB来设置,可以先选按钮的state,然后设置不同的背景图片
+ (UIButton *)buttonWithTitle:(NSString *)title
target:(id)target
selector:(SEL)selector
frame:(CGRect)frame
image:(UIImage *)image
imagePressed:(UIImage *)imagePressed
darkTextColor:(BOOL)darkTextColor
{
UIButton *button = [UIButton alloc] initWithFrame:frame;

button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:UIColor blackColor forState:UIControlStateNormal];

UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];

UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = UIColor clearColor;

return button;
}

UIImage *buttonBackground = UIImage imageNamed:@"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:@"blueButton.png";

CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);

UIButton *button = [FinishedStatsView buttonWithTitle:title
target:target
selector:action
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];

self addSubview:button;
===========================
in your - (void) applicationDidFinishLaunching:(UIApplication *)application
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
Seems helpfull too:
[[UINavigationBar appearance] setTintColor:myColor];
====ipad 1的测试要求,否则其它的要如何适应呢,每个界面写点?
I had the same issue of 
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
which doesn't work on iOS 4 but was working fine on iOS 5. So I tried an alternate solution for it.
image = [UIImage imageNamed:@"yourImageName.png"];if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0{[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];}else{UIImageView *imageView = [[UIImageView alloc] initWithImage:image];[self.navigationController.navigationBar addSubview:imageView];}
总结起来就是,如果ios5或以上,可以使用统一的方式,如果其它,则需要分开在每个的viewload中
 if(![[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS <5.0        {        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar.png"]];        [self.navigationController.navigationBar addSubview:imageView];        [imageView release];    }[/code]
================再看tableview的,如果是间隔设颜色,编辑时候会遇到问题。。
If your tableview is grouped you can use[code]@property(nonatomic) BOOL shouldIndentWhileEditing
if not the other option I know of is to take your background for a cells 1 & 2 and create one resizable image that tiles with
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
and use that as your tableview background. capInsets would of course be all zero.
UIImage *tableBackground =[[UIImage imageNamed:@"cell_1_2"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
If your cells are standard height make a new image in photoshop that is a combination of your cell1 & cell2 background that you want to alternate down the table, 320x88 and call it 'cell_1_2'. Then use that as your tableview background, and it will be tiled/repeat the length of your tableview. The only problem would be if the number of cells in the table didn't take up the height of the screen, the background image would still tile to the bottom of the view. So it may not be the best solution
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: