IT

iOS 7에서 UIButton 이미지를 설정하면 파란색 버튼이 나타납니다.

lottoking 2020. 6. 3. 08:12
반응형

iOS 7에서 UIButton 이미지를 설정하면 파란색 버튼이 나타납니다.


iOS 6 SDK에서 버튼 안에 이미지를 표시하기 위해 다음 코드 줄을 작성했습니다.

NSURL *thumbURL2 = [NSURL URLWithString:@"http://example.com/thumbs/2.jpg"];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];

그러나 이제 Xcode 5 및 iOS 7에서는 작동하지 않습니다. 버튼에 이미지가 없습니다. 버튼은 파란색으로 채워져 있습니다.


iOS7에는 UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS (7_0)라는 새로운 버튼 유형이 있습니다. // 표준 시스템 버튼

.xib 파일을 확인하고 버튼 유형을 사용자 정의로 변경하십시오. 이미지를 봐

프로그래밍 방식으로이 작업을 수행하려면 다음 행을 다음에 추가하십시오 viewDidLoad.

[UIButton buttonWithType:UIButtonTypeSystem]; 

iOS 7에서 버튼의 색조 색상을 표시하기 위해 알파 마스크로 제공된 이미지를 사용하는 것 같습니다. UIButtonTypeCustom나를 위해 트릭을 수행하기 위해 버튼 유형을 변경했습니다 (user716216 덕분에). 내 경우와 같이 이미 배경 이미지가 있으면 이미지를 배경으로 설정하는 것이 항상 작동하지는 않습니다.


스위프트 3 :

let image = UIImage(named: "my-image")
myButton.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)

이미지가있을 수 있으며 이미지를 볼 수 없습니다. 버튼 유형을로 변경해보십시오 UIButtonTypeCustom. 그래도 작동하지 않으면 버튼의 배경색을[UIColor clearColor];


문제는 TintColor입니다. 기본적으로 iOS는 모든 버튼 위에 푸른 색조를냅니다. 당신은 세 가지 방법으로 해결할 수 있습니다.

  1. 색조 색상을 변경하십시오. [button setTintColor:[UIColor blackColor]];이것은 원하지 않는 방식으로 이미지를 채색 할 수 있습니다.

  2. 다른 제안처럼 배경 이미지를 설정하십시오. [button setBackgroundImage:[UIImage...]];

  3. UIImageView를 버튼에 추가하십시오.

UIImageView * img = [[UIImageView alloc] initWithImage:[UIImage...]];

[button addSubView:img];


신속하게 :

    let aButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

나는 같은 문제가 있었다. 스토리 보드에는 이미지가없는 버튼이있었습니다.

그런 다음 코드에서 이미지를 할당합니다.

iOS 7이 왔고 파란 이미지가 많이 생겼습니다.

해결책은 간단하지만 혼란 스러웠습니다. 스토리 보드에 이미지를 할당 한 다음 런타임에 이미지를 변경하면 정상적으로 작동합니다.

You always must specify a starting image on the storyboard even if you are not going to use it.


This worked for me

[myButton1 setBackgroundImage:[UIImage imageNamed:@"phones.png"] forState:UIControlStateNormal];

Note:Remove front image before doing this.


Old thread, but I wanted to chime in because I just had the same problem. The issue was just that you are calling setImage when you should call setBackgroundImage.


None of the given solutions were working for me. If you do not set an initial image in Storyboard, you can still change the image of the button by using setBackgroundImage.

For your example, only a minor change is needed.

NSURL *thumbURL2 = [NSURL URLWithString:@"http://example.com/thumbs/2.jpg"];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setBackgroundImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];

This Problem is called blue color problem of the button in xcode. When we make button by code the button shows the blue tint color by default.This can be solved byt assigning tint color to black or white accordingly to your cell's color. The code is :

UIImage *closebtnimg = [UIImage imageNamed:@"icon_uncheck.png"];
 UIImage *closebtnimg1 = [UIImage imageNamed:@"icon_checked.png"];

Custombutton *button = [Custombutton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(52, 66, 25, 24)];
[button setBackgroundImage:closebtnimg forState:UIControlStateNormal];
[button setBackgroundImage:closebtnimg1 forState:UIControlStateSelected];
[button setTintColor:[UIColor whiteColor]];
[cell.contentView addSubview:button];
[button addTarget:self action:@selector(changeImage:)  forControlEvents:UIControlEventTouchUpInside];

Using Xcode 9.2 none of the above solutions worked for what I was looking for.

I was looking for a solution that will let me set .normal and .selected UIControlState images inside the storyboard for their original rendering mode, but, inside the Swift file, no string literals should exist regarding the image names.

Basically, inside your code you will get the image you set inside your storyboard for .normal state and re-render it as .alwaysOriginal (Same for .selected state), then, you will set that image (which is now rendered as original and won't be affected by the tint) for the relevant state (.normal and .selected) of your UIButton.

Here it is:

// Get your .normal image (you set via your storyboard) and render it as original
let unselectedImage = yourButton.image(for: .normal)?.withRenderingMode(.alwaysOriginal)
// Set your normal image but this time rendered as original
yourButton.setImage(unselectedImage, for: .normal)
// Same for selected state
let selectedImage = yourButton.image(for: .selected)?.withRenderingMode(.alwaysOriginal)
yourButton.setImage(selectedImage, for: .selected)

This way you can set your button image states and if the image name will change, it won't affect your code.


네 가지 상태 (기본, 강조 표시, 선택, 비활성화) 모두에 대해 색조 색상을 선명한 색상으로 만들면 효과가 있습니다.


에서 스위프트 4 , 당신을 초기화 UIButton하고 다음과 같이 uyour 이미지 데이터를 할당 :

let myButton = UIButton(type: .cutsom)
myButton.setImage(UIImage(data:myImageData), for: .normal)

참고 URL : https://stackoverflow.com/questions/18133465/setting-uibutton-image-results-in-blue-button-in-ios-7

반응형