【问题标题】:How do I change both the image and text of an iOS button?如何更改 iOS 按钮的图像和文本?
【发布时间】:2012-12-03 00:02:33
【问题描述】:

我有一个可以正确换出图像的圆形矩形按钮。我还想更改其中的文本,但该文本没有显示。 StackOverflow 上的大多数问题似乎只涉及一个问题,而不是另一个问题,而且看起来最相关的问题最终都针对某种 BarItem。

在按钮触摸的 IBAction 中,我有以下内容。

 [sender setImage:[UIImage imageNamed:@"swappingCorrectly.png"] forState:UIControlStateNormal];
 [sender setTitle:(@"Aww, this doesn't show up!") forState:UIControlStateNormal];

如前所述,图像切换得很好——但没有显示该标签。我在另一个不交换图像的按钮中使用了相同的文本交换行,它工作正常。

它只是在第一次点击后不显示。有谁知道我可以做些什么来显示该文本?

【问题讨论】:

  • 您是否也在某处设置了属性标题?
  • 正在做 [sender setAttributedTitle:(@"What!")];导致“NSInvalidArgumentException”,原因:“-[UIAccessibilityBundle setAttributedTitle:]: unrecognized selector sent to instance”异常,恐怕。
  • 它是[UIButton setAttributedTitle:forState:],与setTitle 的签名相同。只询问,因为属性标题将优先于标题。
  • ^^ 仅供参考 适用于 iOS 6.0 及更高版本。

标签: ios xcode image button text


【解决方案1】:

这对我有用。

- (IBAction)buttonPressed:(id)sender {
    [sender setImage:[UIImage imageNamed:@"button.jpeg"] forState:UIControlStateNormal];
    [sender setTitle:(@"Aww") forState:UIControlStateNormal];
}

我确实注意到标签在最右边,而不是死点。标签是否可能在错误的位置呈现?注释掉 setTitle 会产生相同的结果,按钮标题会被图像偏移。

如何将 UIImageView 变成按钮。注意:这很脏,应该包装到一个类中。我离开了,但注释掉了代码,只是为了做点什么。未显示的是在 xib 文件中抛出并设置为 userenabled = true 的 UIImageView。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonPressed:)];
    [[self fakeButton] addGestureRecognizer:tap];

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 25)];
    label.text = @"unpressed";
    label.backgroundColor = [UIColor clearColor];
    [[self fakeButton] addSubview:label];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonPressed:(id)sender {
//    UIGestureRecognizer *gesture = (UIGestureRecognizer*)sender;
//    UIImageView* temp = (UIImageView*)gesture.view;
//    for (UILabel *label in temp.subviews) {
//        label.text = @"pressed";
//    }
//    temp.image = [UIImage imageNamed:@"button.jpeg"];
//    
//    //if you want it to reset after a Delay
//    [self performSelector:@selector(buttonUnpressed:) withObject:temp afterDelay:.2];

}

-(void)buttonUnpressed:(UIImageView*)view
{

//    for (UILabel *label in view.subviews) {
//        label.text = @"unpressed";
//    }
//    view.image = [UIImage imageNamed:@"origButton.jpeg"];
//    [self performSelector:@selector(buttonUnpressed:) withObject:view afterDelay:.2];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    if (CGRectContainsPoint([[self fakeButton] frame], point))
    {
            //Do touch down state
        NSLog(@"touchStarted");
        [[self fakeButton] setImage:[UIImage imageNamed:@"button.jpeg"]];
        for (UILabel *label in [[self fakeButton] subviews]) {
            label.text = @"pressed";
        }
    }

}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch ended");
    //remove touch down state
    [[self fakeButton] setImage:[UIImage imageNamed:@"origButton.jpeg"]];
    for (UILabel *label in [[self fakeButton] subviews]) {
        label.text = @"unpressed";
    }

}

@end

【讨论】:

  • 有趣。我的按钮占据了整个区域,因此如果有偏移量将其从屏幕上移开,那么它的缺失将是有意义的。将实验,谢谢!另外,考虑一下我在交换之前和之后都有一张图片。
  • 我会尝试的几件事是设置边缘偏移 [sender setTitleEdgeInsets:UIEdgeInsetsMake(-10.0, 10.0, 0.0, 0.0)];或添加一个新标签并通过触摸。我个人讨厌自定义它们,通常只使用 UIImageView 并向其添加手势和状态,因此我有更多的控制权。
  • 我之前实际上是在使用 UIImageView 和一个具有不同图像的非交换按钮。但是,当按下并使其隐藏时,它将不再被按下 - 就好像在按钮隐藏后图像优先。如果你有一篇关于如何使用 UIImageView 的博文,尽管我现在承诺我的缺失文本,请随时链接它!
  • 查看我的答案所附的课程。
  • 我相信你,因为我在一段时间内不会重新访问它来验证它。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 2014-06-15
  • 2011-06-24
  • 2013-06-25
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多