Tim*_*mes 20
我读了你的问题,要求(像我一样)以编程方式设置按钮颜色.这是UIKit的一个明显的遗漏,我无法让无证的UIGlassButton工作.
我用一个段解决了这个问题UISegmentedControl,它允许你设置tintColor:
UISegmentedControl * btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:name]];
btn.momentary = YES;
btn.segmentedControlStyle = UISegmentedControlStyleBar;
btn.tintColor = color;
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)
请注意,momentary和segmentedControlStyle属性确实很重要,并且可以使用图像而不是NSString *名称.
如果您可以使用一组有限的固定图像,带有端盖的可伸缩图像可以正常工作,但这不符合要求!例如,
buttonImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:16];
Run Code Online (Sandbox Code Playgroud)
Bra*_*son 16
我发现我需要使用可伸缩的图像来实现这一目标.Apple的UICatalog示例有一个或多个以这种方式绘制的彩色按钮.您可以使用他们的模板图像并重新着色以满足您的按钮需求.
我不确定在Interface Builder中这样做,但我能够使用以下代码创建一个按钮并使用图像作为其内容:
downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(36, 212, 247, 37)];
downloadButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
downloadButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[downloadButton setTitle:NSLocalizedStringFromTable(@"Download", @"Localized", nil) forState:UIControlStateNormal];
[downloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[downloadButton setFont:[UIFont boldSystemFontOfSize:14.0]];
UIImage *newImage = [[UIImage imageNamed:@"greenButton.png"] stretchableImageWithLeftCapWidth:12.0f topCapHeight:0.0f];
[downloadButton setBackgroundImage:newImage forState:UIControlStateNormal];
[downloadButton addTarget:self action:@selector(downloadNewItem) forControlEvents:UIControlEventTouchDown];
downloadButton.backgroundColor = [UIColor clearColor];
[downloadDisplayView addSubview:downloadButton];
Run Code Online (Sandbox Code Playgroud)
los*_*sit 15
为圆角矩形按钮设置视图的背景颜色不会更改按钮的背景颜色.尝试将按钮设置为自定义按钮(检查器中的第一个下拉菜单),然后设置背景颜色.你会得到想要的效果:)
ale*_*lex 10
看来,按钮颜色仍然是一个问题.以下类别通过采用UIColor参数的实例方法以编程方式完全设置按钮的backgroundimage.无需创建按钮backgroundimage文件.它保留基于UIControlState的按钮行为.它保持圆角.
头文件UIButton + ColoredBackground.h
#import <UIKit/UIKit.h>
@interface UIButton (ColoredBackground)
- (void)setBackgroundImageByColor:(UIColor *)backgroundColor forState:(UIControlState)state;
@end
Run Code Online (Sandbox Code Playgroud)
以及UIButton + ColoredBackground.m的内容
#import "UIButton+ColoredBackground.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIButton (ColoredBackground)
- (void)setBackgroundImageByColor:(UIColor *)backgroundColor forState:(UIControlState)state{
// tcv - temporary colored view
UIView *tcv = [[UIView alloc] initWithFrame:self.frame];
[tcv setBackgroundColor:backgroundColor];
// set up a graphics context of button's size
CGSize gcSize = tcv.frame.size;
UIGraphicsBeginImageContext(gcSize);
// add tcv's layer to context
[tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
// create background image now
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// set image as button's background image for the given state
[self setBackgroundImage:image forState:state];
UIGraphicsEndImageContext();
// ensure rounded button
self.clipsToBounds = YES;
self.layer.cornerRadius = 8.0;
[tcv release];
}
@end
Run Code Online (Sandbox Code Playgroud)
在每个UIButton实例上调用新方法,如:
[myButton setBackgroundImageByColor:[UIColor greenColor] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
在按钮属性选项板中将按钮类型设置为"自定义".这将为您提供一个方形按钮,其中包含您为背景选择的任何颜色.
如果你想要一个看起来更像传统按钮但有不同颜色的按钮,你需要进入某种图像编辑软件并创建它(我使用photoshop进行自定义按钮).
| 归档时间: |
|
| 查看次数: |
84903 次 |
| 最近记录: |