완료 단추를 나타 내기 위해 사용자 지정 UIBarButtonItem을 만들고 싶습니다. 앱을 국제화 할 수 있기를 원하며 텍스트를 직접 편집 할 수 있기를 원합니다. "Done"라벨의 길이가 다른 것을 고려하기 위해 stretchableImage를 디자인했습니다. 기본 편집 단추 배경을 직접 변경할 수 있습니까? 아니면 사용자 정의 UIBarButtonItem이 필요합니까? 그렇다면 레이블의 길이에 따라 배경 stretchableImage의 크기를 동적으로 조정하는 방법은 무엇입니까?완료 UIBarButtonItem의 배경 이미지
답변
iOS 5.0만을 타겟팅하는 경우 새로운 UIAppearance
방법 인 -setBackButtonBackgroundImage:forState:barMetrics:
을 사용하여 기본 모양을 구체적으로 변경할 수 있습니다. 당신은 이전 버전의 iOS를 지원해야하는 경우
, 당신은 UIButton
인스턴스 변수를 추가, UIBarButtonItem
를 서브 클래스를 작성하고 UIBarButtonItem
의 init
방법에 –initWithCustomView:
를 호출해야합니다. UIBarButtonItem
은 UIView
의 하위 클래스가 아니며 사용자 정의 이미지를 그릴 수 없기 때문입니다. 또한 UIBarButtonItem
의 width
속성을 수동으로 설정해야합니다.
@interface MYCustomBarButtonItem : UIBarButtonItem
{
UIButton *button;
}
- (id)initWithTitle:(NSString *)title; // name it in concordance with your needs
@end
#define kButtonHeight 30
#define kButtonTitleFontSize 12
#define kButtonTitlePadding 5
@implementation MYCustomBarButtonItem
- (id)initWithTitle:(NSString *)title
{
button = [UIButton buttonWithType:UIButtonTypeCustom]; // add retain here, and write the dealloc method if you aren't using ARC. Also, release it if self == nil.
self = [super initWithCustomView:button];
if (self) {
UIFont *titleFont = [UIFont boldSystemFontOfSize:kButtonTitleFontSize];
CGSize titleSize = [title sizeWithFont:titleFont];
CGFloat buttonWidth = titleSize.width + kButtonTitlePadding * 2;
button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
self.width = buttonWidth;
[button setTitle:title forState:UIControlStateNormal];
// Set your styles
button.titleLabel.font = titleFont;
// Normal state background
UIImage *backgroundImage = ...; // create your normal stretchable background image
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
// Pressed state background
backgroundImage = ...; // create your pressed stretchable background image
[button setBackgroundImage:backgroundImage forState:UIControlStateHighlighted];
// and so on...
}
return self;
}
@end
ps. button
인스턴스로 작업하려면 하위 클래스의 target
및 action
속성을 무시해야합니다.
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"I am done" style:UIBarButtonItemStyleBordered target:self action:nil];
UIImage *stretchable = [[UIImage imageNamed:@"StretchableImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:16];
[btnDone setBackgroundImage:stretchable forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setRightBarButtonItem:btnDone];
UIBarButtonItem가 자동으로 라벨의 폭 폭의 조정 않습니다
내가 질문을 잘못 이해했을 수 있습니다,하지만 난 당신이 원하는 모든 생각이입니다.
@Stanislav Yaglo가 맞습니다. 이 솔루션은 iOS 5에서만 작동합니다. 이전 버전을 타겟팅하려면 UIBarButtonItem의 간단한 하위 클래스를 만들어야합니다. – simonbs
- 1. UIBarButtonItem의 텍스트를 어떻게 설정합니까?
- 2. 배경 작업자 실행 작업 완료
- 3. 트리거 배경 작업자 완료 이벤트
- 4. CSS 배경 이미지 스트레치 배경 이미지 속성
- 5. 이미지 맵의 배경 이미지
- 6. iOS 배경 이미지 업로드
- 7. 배경 이미지
- 8. 이미지 배경
- 9. 배경 이미지
- 10. 이미지 대 UIButton의 배경 이미지
- 11. 배경 이미지 위에 이미지 회전
- 12. 여러 이미지 선택 배경 이미지
- 13. UIBarButtonItem의 터치 업 효과
- 14. UIBarButtonItem의 활성화를 애니메이션화 하시겠습니까?
- 15. UIBarButtonItem의 캘린더 아이콘
- 16. jQuery 애니메이션 + 배경 이미지 스왑
- 17. XAML 이벤트로 배경 이미지 렌더링
- 18. 코코아 애니메이션 이미지 감지 완료
- 19. 이미지 소스 완료 숭고한 텍스트 2
- 20. 배경 이미지 fadeIn 문제
- 21. GWT CSS 배경 이미지
- 22. 본문 배경 이미지 링크
- 23. LinearLayout의 배율 배경 이미지
- 24. tile 배경 이미지 css
- 25. 탐색 모음 배경 이미지
- 26. 배경 이미지 너비를 얻으려면
- 27. 배경 이미지 센터링?
- 28. 배경 이미지 창
- 29. 배경 이미지 연결
- 30. 갤러리 이미지 배경
텍스트의 너비에 따라 단추 크기를 조정하는 방법은 무엇입니까? 또한 배경을 stretchableImage로 설정하는 방법은 무엇입니까? 감사. –
'NSString'의'-sizeWithFont :'메소드를 사용합니다 : http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html. 배경은'UIButton'의'-setBackgroundImage : forState :'로 설정됩니다. –
@ibeitia, 코드를 업데이트했습니다. –