How to get a background image of a button on an iPhone?

I am currently working in an iPhone application, I created two buttons and set the image for both buttons, when the user pressed button2, and then displayed button1 inside button2, I tried my level better, but I can’t fix it, please somebody it helps me

Thanks at Advance

I tried this:

btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; [btn1 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal]; btn1.frame=CGRectMake(61, 60, 50, 50); [btn1 addTarget:self action:@selector(Button1Method) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1]; btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; [btn2 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal]; btn2.frame=CGRectMake(112, 60, 50, 50); [btn2 addTarget:self action:@selector(Button2Method) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; -(void)Button2Method { [btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal]; } 
+4
source share
3 answers

Try the following: -

 [btn2 setImage:btn1.currentImage forState:UIControlStateNormal]; 

And currentBackgroundImage is a UIImage , and you are treating it as an NSString , this is wrong.

+6
source

Use this

  UIImage *img = btn1.currentImage; [btn2 setImage:img forState:UIControlStateNormal]; 
+1
source

This is because you set the image to the btn1 setImage: button and get access to the btn1.currentBackgroundImage background image

change your code tested

 -(void)Button2Method { [btn2 setImage:btn1.imageView.image forState:UIControlStateNormal]; } 
0
source

Source: https://habr.com/ru/post/1435621/


All Articles