按下UIButton的UIControlState是什么? [英] What's the right UIControlState for the UIButton being pressed?

查看:180
本文介绍了按下UIButton的UIControlState是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里以编程方式自定义 UIButton

I'm customizing the UIButton programmatically here:

button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setSelected:YES];
        button.frame = CGRectMake(x, y, width, height);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [button setTitle:@"Button Title" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateDisabled];

问题是如果我按住图像背景图像消失,直到我释放它。 ..

The issue is if I'm holding down the image background image disappears until I'm releasing it...

推荐答案

我认为你处于过度杀伤模式:)。尝试为UIControlStateNormal设置button.png,为UIControlStateHighlighted设置buttonActive.png。不需要休息。看看是否有效。

I think you're in overkill mode :). Try setting button.png for UIControlStateNormal and buttonActive.png for UIControlStateHighlighted. No need for the rest. See if this works.

编辑:

另外,请记住:图像文件名区分大小写

您是否在设备上进行测试?图像名称区分大小写,但不适用于模拟器。例如,如果您的实际图像文件名为buttonactive.png,但您从代码中将其称为buttonActive.png,它将显示在模拟器上,但不会显示在设备上。

Are you testing on device? Image names are case sensitive for device builds, but not for the simulator. For example, if your actual image file is named buttonactive.png, but you're calling it as buttonActive.png from your code, it will show up on the simulator, but not on the device.

请确保两个图片名称的大小写与实际文件的名称相符。

Please ensure that the case for both image names matches the name of the actual file.

编辑#2:

试试这段代码

button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];

这篇关于按下UIButton的UIControlState是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆