Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This works for me.</p> <pre><code>- (IBAction)buttonPressed:(id)sender { [sender setImage:[UIImage imageNamed:@"button.jpeg"] forState:UIControlStateNormal]; [sender setTitle:(@"Aww") forState:UIControlStateNormal]; } </code></pre> <p><img src="https://i.stack.imgur.com/hiKGD.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/Hv9Wa.png" alt="enter image description here"></p> <p>I did notice that the label is to the far right as opposed to dead center. Is it possible the label is rendering in the wrong spot? Commenting out the setTitle yields the same results with button title being offset by the image.</p> <p>How to turn a UIImageView into a button. Note: this is very dirty and should be wrapped into a class. I left in but commented out the code to just do something on tap. Whats not shown is the UIImageView that was thrown on a xib file and set to userenabled = true.</p> <pre><code>#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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload