代码设置按钮不同状态的颜色
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 shouldIndentWhileEditingif 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)capInsetsand 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
相关文章推荐
- 【安卓开发】JAVA代码设置selector不同状态下的背景颜色
- JAVA代码设置selector不同状态下的背景颜色
- 使用selector设置按钮不同状态的样式(Android)
- 代码实现 按钮点击出现不同的图片或者颜色
- Android selector的用法及设置按钮不同状态下的背景图片
- Android设置Selector不同状态下颜色及图片
- 应用键横竖屏切换;label中显示图片;不同类型设备适配的代码;UIWebView字体大小、字体颜色、背景色的设置;
- 导航栏标题的设置以及左右按钮的颜色、选取照片弹出UIImagePickView时状态栏上移问题、View向上旋转某个角度
- Android设置Selector不同状态下颜色及图片
- Android利用selector设置按钮不同状态下的背景图片
- 设置表格奇偶行不同颜色,鼠标划过变色的javascript代码
- iOS开发:在XIB中设置按钮不同点击状态下的不同图标显示
- 代码设置按钮状态
- iOS小技巧 - 为按钮设置不同状态下的背景色
- extjs ColumnChart设置不同的颜色实现代码
- java代码块之简易qq登录界面及按钮颜色设置代码
- 设置链接的不同状态下的颜色
- [置顶] android,在java代码中,如何给textview设置不同的颜色,以及引用color中资源设置颜色
- iOS小技巧 - 为按钮设置不同状态下的背景色
- extjs ColumnChart设置不同的颜色实现代码