2013-02-20 1 views
0

UIButton에 이미지를 설정했지만이 이미지 위에 제목을 설정해야합니다. 어떻게해야합니까?이미지가있는 UIButton의 제목

[button setImage:[UIImage imageNamed:@"btn_select_s.png"] forState:0]; 
[button setImage:[UIImage imageNamed:@"btn_select_p.png"] forState:1]; 
[button setTitle:@"my title" forState:0]; 
+0

[iPhone SDK : UIButton과 이미지 및 텍스트 모두 가능합니까?] (http://stackoverflow.com/questions/4344847/iphone-sdk-uibutton-with-both-image-and-text-possible) – rishi

답변

1

는 대신 버튼의 배경 이미지를 설정해야합니다

[button setBackgroundImage:[UIImage imageNamed:@"foo"] forState:UIControlStateNormal]; 
+0

Thx 많이! 작동합니다 – Ladessa

4

당신은 당신이 원하는대로 제목과 이미지의 위치를있는 UIButton의 titleEdgeInsets 및 imageEdgeInsets를 사용할 수 있습니다.
https://github.com/lionhylra/FlexButton

그것은 같은 기능을 가지고 있습니다 :

[self setImageEdgeInsets:UIEdgeInsetsMake(imageFrame.origin.y, imageFrame.origin.x, 0, 0)]; 
[self setTitleEdgeInsets:UIEdgeInsetsMake(labelFrame.origin.y, labelFrame.origin.x, 0, 0)]; 
0
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop]; 

//Use you values for edge insets here. 
[button setTitleEdgeInsets:UIEdgeInsetsMake(20.0f, 10.0f, 0.0f, 0.0f)]; 
-1

여기에 편리한있는 UIButton 서브 클래스가하십시오있는 UIButton 서브 클래스의 예를 들어

See Apple's UIButton reference documentation

, 는 - 그것은 4 가지 레이아웃 스타일을 제공합니다.
- UIButton처럼 간단하게 작동합니다.
- 버튼의 크기를 조정하면 imageView 및 titleLabel의 크기가 적절하게 변경됩니다. 그들은 혼란에 빠지지 않을 것입니다. 이것은 가장자리 삽입을 사용하는 것의 근본적인 차이입니다.
- TitleLabel 및 imageView는 항상 수직 및 수평 중심에 배치됩니다.

+0

답변 만 Swift에서 예제를 제공합니다. 질문은 특히 Objective-C로 태그가 지정됩니다. Objective-C 기반 응답으로 예제를 게시하는 것을 고려하십시오. – Acludia

관련 문제