繁体   English   中英

如何更改iOS按钮的图像和文本?

[英]How do I change both the image and text of an iOS button?

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

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

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

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

只是在第一次点击后没有显示。 有人知道我该怎么做才能使该文本显示出来吗?

这对我有用。

- (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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM