ios 触摸时 UIButton 不显示高亮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21133856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:08:26  来源:igfitidea点击:

UIButton not showing highlight when touched

iosobjective-cuibutton

提问by HurkNburkS

I am trying to highlight my UIButton with a custom UIColor light grey and make the text go white. However, I am not sure how to do this; this is how far I have gotten with creating the UIButton.

我试图用自定义的 UIColor 浅灰色突出显示我的 UIButton 并使文本变为白色。但是,我不知道该怎么做;这是我在创建 UIButton 方面取得的进展。

cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelButton.frame = CGRectMake(10.0, calculatorView.frame.size.height-55, 100.0, 45.0);
    [cancelButton addTarget:self action:@selector(calculateMethod:)forControlEvents:UIControlEventTouchDown];
    [[cancelButton titleLabel] setFont:[UIFont fontWithName: @"Super-text" size:20.0]];
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];

回答by Christian A. Str?mmen

To get the white text on highlight just do:

要获得突出显示的白色文本,只需执行以下操作:

[cancelButton setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateHighlighted];

To get the background to change you need to either do setBackgroundImage:forState: and use a UIImage with the pattern color, or subclass UIButton and set the appropriate background color in the setHighlight: method.

要更改背景,您需要执行 setBackgroundImage:forState: 并使用带有图案颜色的 UIImage,或子类 UIButton 并在 setHighlight: 方法中设置适当的背景颜色。

EDIT: Swift 2.x version

编辑:Swift 2.x 版本

cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)

EDIT: Swift 3.0 version

编辑:Swift 3.0 版本

cancelButton.setTitleColor(.white, for: .highlighted)

回答by nitin kachhadiya

You can create an image to fill in the background with color.

您可以创建一个图像来填充背景颜色。

[cancelButton setBackgroundImage:[self setBackgroundImageByColor:[UIColor blueColor] withFrame:cancelButton.frame cornerRadius:0] forState:UIControlStateHighlighted];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

This method builds an image the size of the button with a solid background of the color backgroundColor.

此方法构建一个按钮大小的图像,并带有 backgroundColor 颜色的纯色背景

-(UIImage *)setBackgroundImageByColor:(UIColor *)backgroundColor withFrame:(CGRect )rect cornerRadius:(float)radius{

    UIView *tcv = [[UIView alloc] initWithFrame:rect];
    [tcv setBackgroundColor:backgroundColor];

    CGSize gcSize = tcv.frame.size;
    UIGraphicsBeginImageContext(gcSize);
    [tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);;
    [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];
    [image drawInRect:RECT];
    UIImage* imageNew = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageNew;
}

回答by Merricat

I know you wanted a white highlight color, but for anyone else that doesn't care specifically what color the highlight is, just switch the button type to System(in the Attribute Inspector). The nice thing is that you can still use custom fonts, colors, etc.

我知道你想要一个白色的高光颜色,但对于其他不关心高光是什么颜色的人,只需将按钮类型切换到系统(在属性检查器中)。好消息是您仍然可以使用自定义字体、颜色等。

回答by mikeho

The answer by @nitin kachhadiya is very good, but I tweaked it, simplified it, and made it a category of UIButton. Add these two files to your project. Then to choose the color you want to set:

@nitin kachhadiya 的答案非常好,但我对其进行了调整、简化,并使其成为 UIButton 的一个类别。将这两个文件添加到您的项目中。然后选择要设置的颜色:

[button setHighlightedColor:[UIColor blueColor]];

UIButton+FJHighlighted.h

UIButton+FJHighlighted.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UIButton (FJHighlighted)

- (void)setHighlightedColor:(UIColor *)highlightColor;

@end

UIButton+FJHighlighted.m

UIButton+FJHighlighted.m

#import "UIButton+FJHighlighted.h"

@implementation UIButton (FJHighlighted)

- (void)setHighlightedColor:(UIColor *)highlightColor {
    [self setBackgroundImage:[self setBackgroundImageByColor:highlightColor cornerRadius:0]
                    forState:UIControlStateHighlighted];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}

- (UIImage *)setBackgroundImageByColor:(UIColor *)backgroundColor cornerRadius:(float)radius {
    UIView *tcv = [[UIView alloc] initWithFrame:self.frame];
    [tcv setBackgroundColor:backgroundColor];

    CGSize gcSize = tcv.frame.size;
    UIGraphicsBeginImageContext(gcSize);
    [tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);;
    [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];
    [image drawInRect:RECT];
    UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageNew;
}

@end

回答by Praveen S

Use titleColor:forState:to set the color of text on selection/highlight/disable for the respective states.

使用titleColor:forState:为各个状态设置选择/突出显示/禁用文本的颜色。