코드에서 Uibutton의 이미지를 설정합니다
-
16-09-2019 - |
문제
코드에서 Uibutton의 이미지를 어떻게 설정합니까?
나는 이것을 가지고있다:
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];
그러나 이미지를 설정하는 것이 무엇인지 알지 못합니다.
해결책
대상 c
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
스위프트 4
let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)
다른 팁
Mike의 솔루션에는 이미지가 표시되지만 제목이나 이미지를 설정할 수 있으므로 버튼에있는 제목이 표시되지 않습니다.
(이미지와 제목)를 모두 설정하려면 다음 코드를 사용하십시오.
btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];
이 작업이 작동하기 전에 이미지 프레임 크기를 기반으로 버튼 프레임을 명시 적으로 크기를 조정해야했습니다.
UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;
[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController
action:@selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:listButton];
self.navigationItem.leftBarButtonItem = jobsButton;
신속한 사용자의 경우
// case of normal image
let image1 = UIImage(named: "your_image_file_name_without_extension")!
button1.setImage(image1, forState: UIControlState.Normal)
// in case you don't want image to change when "clicked", you can leave code below
// case of when button is clicked
let image2 = UIImage(named: "image_clicked")!
button1.setImage(image2, forState: UIControlState.Highlight)
이렇게 할 수 있습니다
[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"buttonimage.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(btnSendComment_pressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
이미지를 어느 쪽이든 넣을 수 있습니다.
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self
action:@selector(goToOne)
forControlEvents:UIControlEventTouchUpInside];
[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];
//OR setting as background image
[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"]
forState:UIControlStateNormal];
[self.view addSubview:btnTwo];
나는 추가 할 해결책을 찾고 있었다 UIImage
나에게 UIButton
. 문제는 이미지를 필요보다 크게 표시하는 것입니다. 방금 나에게 도움이되었습니다.
_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;
내 표시를 할 때마다 UIImageView
방금 var를 설정했습니다 hidden
에게 YES
또는 NO
. 다른 해결책이있을 수 있지만이 물건과 여러 번 혼란스러워서 해결했고 내부 물건을 다룰 필요가 없었습니다. UIButton
백그라운드에서하고 있습니다.
코드에서 버튼을 프레임하는 것을 너무 걱정하지 마십시오. 스토리 보드에서 그렇게 할 수 있습니다. 이것은 나를 위해 일했다. 한 줄은 ... 더 간단했다.
[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
{
[btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
// other statements....
}
else
{
[btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
// other statements....
}
}
스위프트 3 버전 (butt_img는 이미지로 설정되어 있어야합니다 assets.xcassets 또는 images.xcassets Xcode의 폴더) :
btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)
어쨌든, 버튼의 크기를 채우도록 이미지를 조정하려면 UIImageView
그것에 대해 이미지를 할당하십시오.
let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)