UIButton:设置选定突出显示状态的图像

发布于 2024-10-31 07:11:08 字数 511 浏览 1 评论 0原文

我为按钮的状态“正常”、“突出显示”和“选定”设置了图像,但是当按钮处于选定状态并且我按下/突出显示它时,我没有看到突出显示的图像,而只是看到灰色的图片。 是否可以在选择按钮时设置突出显示状态的图像?

我的代码:

[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

当我这样做时:

[button setSelected:YES];

并按下按钮,“pressed.png”图像不会选择。

I set an images for button's states Normal,Highlighted and Selected, but when the button in selected state and I press/highlight it I didn't see my highlighted image but just grayed picture.
Is it possible to set an image for highlighted state when the button selected?

my code:

[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

when I do:

[button setSelected:YES];

and press the button, the "pressed.png" image doesn't select.

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

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

发布评论

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

评论(15

眼泪淡了忧伤 2024-11-07 07:11:08

我找到了解决方案:需要添加附加行

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

I found the solution: need to add addition line

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
涫野音 2024-11-07 07:11:08

您可以在 Interface Builder 中执行此操作。

选择您想要在 IB 中设置的 UIButton,然后转到属性检查器

在屏幕截图中,我使用的是自定义按钮类型,但这并不重要。

自定义默认

输入图像此处描述

此处输入图像描述

You can do this in Interface Builder.

Select the UIButton you wish to set in IB then go to the attributes inspector.

In the screen shots,I am using a custom button type , but that does not matter.

Custom Default

enter image description here

enter image description here

北陌 2024-11-07 07:11:08

Swift 3

// Default state (previously `.Normal`)
button.setImage(UIImage(named: "image1"), for: [])

// Highlighted
button.setImage(UIImage(named: "image2"), for: .highlighted)

// Selected
button.setImage(UIImage(named: "image3"), for: .selected)

// Selected + Highlighted
button.setImage(UIImage(named: "image4"), for: [.selected, .highlighted])

要设置背景图像,我们可以使用 setBackgroundImage(_:for:)

Swift 2.x

// Normal
button.setImage(UIImage(named: "image1"), forState: .Normal)

// Highlighted
button.setImage(UIImage(named: "image2"), forState: .Highlighted)

// Selected
button.setImage(UIImage(named: "image3"), forState: .Selected)

// Selected + Highlighted
button.setImage(UIImage(named: "image4"), forState: [.Selected, .Highlighted])

Swift 3

// Default state (previously `.Normal`)
button.setImage(UIImage(named: "image1"), for: [])

// Highlighted
button.setImage(UIImage(named: "image2"), for: .highlighted)

// Selected
button.setImage(UIImage(named: "image3"), for: .selected)

// Selected + Highlighted
button.setImage(UIImage(named: "image4"), for: [.selected, .highlighted])

To set the background image we can use setBackgroundImage(_:for:)

Swift 2.x

// Normal
button.setImage(UIImage(named: "image1"), forState: .Normal)

// Highlighted
button.setImage(UIImage(named: "image2"), forState: .Highlighted)

// Selected
button.setImage(UIImage(named: "image3"), forState: .Selected)

// Selected + Highlighted
button.setImage(UIImage(named: "image4"), forState: [.Selected, .Highlighted])
燃情 2024-11-07 07:11:08

我认为这里的大多数海报完全没有抓住要点。我也有同样的问题。最初的问题是关于选定按钮的突出显示状态(组合两个状态),该按钮无法在 IB 中设置,并且会回退到默认状态并发生一些变暗。正如一篇文章提到的,唯一可行的解​​决方案:

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

I think most posters here miss the point completely. I had the same problem. The original question was about the Highlighted state of a Selected button (COMBINING BOTH STATES) which cannot be set in IB and falls back to Default state with some darkening going on. Only working solution as one post mentioned:

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
等数载,海棠开 2024-11-07 07:11:08

就可以了:

如果你有充分的理由这样做,那么添加这些目标

[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];


-(void)buttonTouchDown:(id)sender{
    UIButton *button=(UIButton *)sender;
    if(button.selected){
        [button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateNormal];
    }
}

-(void)buttonTouchUp:(id)sender{
    UIButton *button=(UIButton *)sender;
    [button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
}

If you have a good reason to do that, this will do the trick

add these targets:

[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];


-(void)buttonTouchDown:(id)sender{
    UIButton *button=(UIButton *)sender;
    if(button.selected){
        [button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateNormal];
    }
}

-(void)buttonTouchUp:(id)sender{
    UIButton *button=(UIButton *)sender;
    [button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
}
恋竹姑娘 2024-11-07 07:11:08

在 Swift 3.x 中,您可以通过以下方式设置选择按钮时突出显示的图像:

// Normal state
button.setImage(UIImage(named: "normalImage"), for: .normal) 

// Highlighted state (before button is selected)
button.setImage(UIImage(named: "pressedImage"), for: .highlighted)

// Selected state
button.setImage(UIImage(named: "selectedImage"), for:  .selected)

// Highlighted state (after button is selected)
button.setImage(UIImage(named: "pressedAfterBeingSelectedImage"), 
                for:  UIControlState.selected.union(.highlighted))

In Swift 3.x, you can set highlighted image when button is selected in the following way:

// Normal state
button.setImage(UIImage(named: "normalImage"), for: .normal) 

// Highlighted state (before button is selected)
button.setImage(UIImage(named: "pressedImage"), for: .highlighted)

// Selected state
button.setImage(UIImage(named: "selectedImage"), for:  .selected)

// Highlighted state (after button is selected)
button.setImage(UIImage(named: "pressedAfterBeingSelectedImage"), 
                for:  UIControlState.selected.union(.highlighted))
那小子欠揍 2024-11-07 07:11:08

很快你可以这样做:

button.setImage(UIImage(named: "selected"), 
                forState: UIControlState.selected.union(.highlighted))

In swift you can do:

button.setImage(UIImage(named: "selected"), 
                forState: UIControlState.selected.union(.highlighted))
同展鸳鸯锦 2024-11-07 07:11:08

就我而言,我必须将 UIButton.Type 从 .custom 更改为 .system

并且:

button.setImage(UIImage(named: "unchecked"), for: .normal)
button.setImage(UIImage(named: "checked"), for: [.selected, .highlighted])

处理点击时:

按钮。 isSelected = !button.isSelected

In my case, I have to change the UIButton.Type from .custom to .system

And:

button.setImage(UIImage(named: "unchecked"), for: .normal)
button.setImage(UIImage(named: "checked"), for: [.selected, .highlighted])

When handling tapping:

button.isSelected = !button.isSelected

貪欢 2024-11-07 07:11:08

如果我错了请纠正我。通过这样做,

   [button setSelected:YES];

您可以清楚地更改所选按钮的状态。因此,很自然地,通过您提供的代码,图像将在您的案例中检查所选状态。png

Correct me if I am wrong. By doing

   [button setSelected:YES];

you are clearly changing the state of the buttons as selected. So naturally by the code you have provided the image will that for the selected state in your case checked.png

青巷忧颜 2024-11-07 07:11:08

如果有人想知道这在 Swift 中是如何工作的,这是我的解决方案:

button.setImage("normal.png", forState: .Normal)
button.setImage("highlighted.png", forState: .Highlighted)
button.setImage("selected.png", forState: .Selected)

var selectedHighLightedStates: UIControlState = UIControlState.Highlighted
selectedHighLightedStates = selectedHighLightedStates.union(UIControlState.Selected)
button.setImage("selectedHighlighted.png", forState: selectedHighLightedStates)

If someone's wondering how this works in Swift, here's my solution:

button.setImage("normal.png", forState: .Normal)
button.setImage("highlighted.png", forState: .Highlighted)
button.setImage("selected.png", forState: .Selected)

var selectedHighLightedStates: UIControlState = UIControlState.Highlighted
selectedHighLightedStates = selectedHighLightedStates.union(UIControlState.Selected)
button.setImage("selectedHighlighted.png", forState: selectedHighLightedStates)
起风了 2024-11-07 07:11:08

Swift 3+

button.setImage(UIImage(named: "selected_image"), for: [.selected, .highlighted])

OR

button.setImage(UIImage(named: "selected_image"), for: UIControlState.selected.union(.highlighted))

表示按钮当前处于<​​code>选中状态,然后你触摸它,显示高亮状态。

Swift 3+

button.setImage(UIImage(named: "selected_image"), for: [.selected, .highlighted])

OR

button.setImage(UIImage(named: "selected_image"), for: UIControlState.selected.union(.highlighted))

It means that the button current in selected state, then you touch it, show the highlight state.

物价感观 2024-11-07 07:11:08

我在设置imageView.highlighted = NO;时遇到问题
(设置YES工作正常,图像更改为突出显示的图像)。

解决方案是调用 [imageView setHighlighted:NO];

一切正常。

I had problem setting imageView.highlighted = NO;
(setting YES worked properly and the image changed to the highlighted one).

The solution was calling [imageView setHighlighted:NO];

Everything worked properly.

吾家有女初长成 2024-11-07 07:11:08

您想从

[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

方法中调用的位置:

-(void)test:(id)sender{
    UIButton *button=(UIButton *)sender;
    if (_togon){
      [button setTitleColor:UIColorFromRGB(89,89, 89) forState:UIControlStateNormal];
      _togon=NO;
    }
    else{
      [button setTitleColor:UIColorFromRGB(28, 154, 214) forState:UIControlStateNormal];
      _togon=YES;
    }
}

上面的内容归功于 Jorge,但我对其进行了一些改进,提供了完整的工作解决方案

Where you want to call from

[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

The method:

-(void)test:(id)sender{
    UIButton *button=(UIButton *)sender;
    if (_togon){
      [button setTitleColor:UIColorFromRGB(89,89, 89) forState:UIControlStateNormal];
      _togon=NO;
    }
    else{
      [button setTitleColor:UIColorFromRGB(28, 154, 214) forState:UIControlStateNormal];
      _togon=YES;
    }
}

Credit above to Jorge but I improved it a bit giving the full working solution

素食主义者 2024-11-07 07:11:08

Xamarin C#

执行按位 OR 由于某种原因不起作用

button.SetImage(new UIImage("ImageNormal"), UIControlState.Normal);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Selected | UIControlState.Highlighted | UIControlState.Focused);

以下作品

button.SetImage(new UIImage("ImageNormal"), UIControlState.Normal);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Selected);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Highlighted);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Focused);

Xamarin C#

Doing bitwise OR doesn't work for some reason

button.SetImage(new UIImage("ImageNormal"), UIControlState.Normal);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Selected | UIControlState.Highlighted | UIControlState.Focused);

The following works

button.SetImage(new UIImage("ImageNormal"), UIControlState.Normal);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Selected);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Highlighted);
button.SetImage(new UIImage("ImagePressed"), UIControlState.Focused);
暗地喜欢 2024-11-07 07:11:08

如果您还需要在按住所选状态的自定义按钮时操作系统默认提供的突出显示色调,请使用此 UIButton 子类。用 Swift 5 编写:

import Foundation
import UIKit
class HighlightOnSelectCustomButton: UIButton {
    override var isHighlighted: Bool {
        didSet {
            if (self.isSelected != isHighlighted) {
                self.isHighlighted = self.isSelected
            }
        }
    }
}

If you need the highlighted tint which the OS provides by default when you tap and hold on a custom button for the selected state as well, use this UIButton subclass. Written in Swift 5:

import Foundation
import UIKit
class HighlightOnSelectCustomButton: UIButton {
    override var isHighlighted: Bool {
        didSet {
            if (self.isSelected != isHighlighted) {
                self.isHighlighted = self.isSelected
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文