Colors 如何以编程方式制作反转颜色按钮?
iOS 7已经推出一段时间了。有一件事我试着去做,但没有成功,那就是将苹果内置的购买按钮复制到他们的应用商店应用程序中 按钮具有1像素边框,并在高亮显示或选中时反转颜色 我可以制作边框,但无法在没有任何图像的情况下以编程方式制作反转颜色效果 我在网上搜索了一下,但到目前为止没有找到任何有用的东西 有人知道如何在iOS 7上为UIButton制作这种效果吗 提前感谢以下是我得到的:Colors 如何以编程方式制作反转颜色按钮?,colors,ios7,uibutton,Colors,Ios7,Uibutton,iOS 7已经推出一段时间了。有一件事我试着去做,但没有成功,那就是将苹果内置的购买按钮复制到他们的应用商店应用程序中 按钮具有1像素边框,并在高亮显示或选中时反转颜色 我可以制作边框,但无法在没有任何图像的情况下以编程方式制作反转颜色效果 我在网上搜索了一下,但到目前为止没有找到任何有用的东西 有人知道如何在iOS 7上为UIButton制作这种效果吗 提前感谢以下是我得到的: #import <QuartzCore/QuartzCore.h> + (void)setCorn
#import <QuartzCore/QuartzCore.h>
+ (void)setCornerRadius:(CGFloat)radius ofView:(UIView*)view
{
view.layer.cornerRadius = radius;
view.layer.masksToBounds = YES;
}
+ (void)setBorderWidth:(CGFloat)width ofView:(UIView*)view
{
view.layer.borderWidth = width;
}
+ (void)setBorderColor:(UIColor*)color ofView:(UIView*)view
{
view.layer.borderColor = [color CGColor];
}
+ (void)styleButton:(UIButton*)button
{
[Common setBorderWidth:1 ofView:button];
[Common setCornerRadius:5 ofView:button];
[Common setBorderColor:[UIColor blueColor] ofView:button];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
UIGraphicsBeginImageContextWithOptions(button.frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blueColor] setFill];
CGContextFillRect(context, CGRectMake(0, 0, button.frame.size.width, button.frame.size.height));
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setBackgroundImage:image forState:UIControlStateHighlighted];
}
#导入
+(void)setCornerRadius:(CGFloat)视图半径:(UIView*)视图
{
view.layer.cornerRadius=半径;
view.layer.masksToBounds=是;
}
+(void)setBorderWidth:(CGFloat)视图宽度:(UIView*)视图
{
view.layer.borderWidth=宽度;
}
+(void)setBorderColor:(UIColor*)视图的颜色:(UIView*)视图
{
view.layer.borderColor=[color CGColor];
}
+(无效)样式按钮:(UIButton*)按钮
{
[Common setBorderWidth:1 of View:button];
[Common setCornerRadius:5 of View:按钮];
[Common setBorderColor:[UIColor blueColor]of视图:按钮];
[按钮设置标题颜色:[UIColor blueColor]用于状态:uicontrol状态正常];
[按钮设置标题颜色:[UIColor whiteColor]用于状态:uicontrol状态高亮显示];
UIGraphicsBeginImageContextWithOptions(button.frame.size,是,0);
CGContextRef context=UIGraphicsGetCurrentContext();
[[UIColor blueColor]setFill];
CGContextFillRect(上下文,CGRectMake(0,0,button.frame.size.width,button.frame.size.height));
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
[按钮设置背景图像:状态图像:uicontrol状态高亮显示];
}
请注意,这仅适用于UIButtonTypeCustom
按钮
当然,如果您在应用程序中使用另一个
tintColor
,请替换[UIColor blueColor]
当按钮处于“选定”状态时,您可以在IOS中免费获得此功能
您需要为按钮的touchUpInside操作创建一个例程。在该例程中,必须切换“selected”属性
- (IBAction)touchedFreeButton:(UIButton *)sender {
self.freeButton.selected = !self.freeButton.selected;
}
我最终将UIButton子类化,并以完全相同的方式实现。我还挫败了UIControlStateNormal的GroundImage。我投了反对票,因为Levinson Technologies应该是正确的答案。我已经测试过了,效果很好。这显然是我们的初衷。你知道如何避免这种行为吗?我只想在不改变颜色的情况下拍摄不同的图像。