使 UIButton 看起来禁用的任何其他选项?

发布于 2025-01-03 17:42:47 字数 293 浏览 0 评论 0原文

我正在尝试使用以下代码使 UIButton 看起来已禁用:

btnYourButton.alpha = 0.6f;
btnYourButton.enabled = NO;

虽然启用了制作(当然也看起来已启用)

btnYourButton.alpha = 1.0f;
btnYourButton.enabled = YES;

,但没有任何方法可以执行这两个操作(使 UIButton 禁用/启用并且还使它看起来在一条语句中禁用/启用)?

I am trying the following code to make a UIButton look Disabled :

btnYourButton.alpha = 0.6f;
btnYourButton.enabled = NO;

While making is enabled(and of course to look enabled)

btnYourButton.alpha = 1.0f;
btnYourButton.enabled = YES;

Isn't there any way, by which I can perform both of this (making UIButton disable/enable and also to make it look disable/enable) in a single statement ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪 2025-01-10 17:42:47

或者您可以尝试子类化 UIButton,例如:

file MyKindOfButton.h

#import <UIKit/UIKit.h>

@interface MyKindOfButton : UIButton

@end

file MyKindOfButton.m

#import "MyKindOfButton.h"

@implementation MyKindOfButton

- (void)setEnabled:(BOOL)enabled {

    [super setEnabled: enabled];

    if (enabled) {
        self.alpha = 1.0f;
    } else {
        self.alpha = 0.6f;

    }
}

@end

Or you could try subclassing UIButton, something like:

file MyKindOfButton.h

#import <UIKit/UIKit.h>

@interface MyKindOfButton : UIButton

@end

file MyKindOfButton.m

#import "MyKindOfButton.h"

@implementation MyKindOfButton

- (void)setEnabled:(BOOL)enabled {

    [super setEnabled: enabled];

    if (enabled) {
        self.alpha = 1.0f;
    } else {
        self.alpha = 0.6f;

    }
}

@end
雨夜星沙 2025-01-10 17:42:47

我知道这是一个非常古老的问题,但这是一个非常好的解决方案。只需创建一个 UIColor 类别并添加此方法即可。

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, (CGRect){.size = size});

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color
{
    return [UIImage imageWithColor:color size:CGSizeMake(1, 1)];
}

现在您只需将背景图像设置为您想要的任何颜色,它就会自动为您处理禁用外观。

[button setTitleColor:[UIColor someColor] forState:UIControlStateNormal];

I know this is a very old question, but here is a very nice solution. Just create a category of UIColor and add this method.

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, (CGRect){.size = size});

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color
{
    return [UIImage imageWithColor:color size:CGSizeMake(1, 1)];
}

Now you can just set the backgroundImage to whatever color you want and it will automatically handle the disable appearance for you.

[button setTitleColor:[UIColor someColor] forState:UIControlStateNormal];
相对绾红妆 2025-01-10 17:42:47

另一种选择是更改禁用状态的文本颜色(例如,浅灰色)。在故事板编辑器中,从“状态配置”弹出按钮中选择“禁用”,然后使用“文本颜色”弹出按钮更改文本颜色。在代码中,使用 -setTitleColor:forState: 消息。

(我意识到这是一篇较旧的帖子,但我认为其他人可能会喜欢这个选项)

Another option is to change the text color (to light gray for example) for the disabled state. In the storyboard editor, choose Disabled from the State Config popup button, and use the Text Color popup button to change the text color. In code, use the -setTitleColor:forState: message.

(I realize this is an older post, but I thought others might like this option)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文