iOS 에서 UIButton 의 3 대 UIEdgeInsets 속성 용법 을 자세히 설명 합 니 다.
UIEdgeInsets 가 뭐 예요?우리 눌 러 서 들 어가 보 자.
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
UIEdgeInsets 는 구조 체 형식 입 니 다.안에 네 개의 인자 가 있 는데 그것 이 바로 top,left,bottom,right 이다.이 네 개의 매개 변 수 는 위의 경계,왼쪽 경계,아래 경계,오른쪽 경계 와 의 거 리 를 나타 낸다.UIEdgeInsets 속성 세 개
여러분 이 발견 하 셨 는 지 모 르 겠 지만 UIButton 에는 세 개의 UIEdgeInsets 속성 이 있 습 니 다.각각:
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // default is UIEdgeInsetsZero
contentEdgeInsets 뒤에 UI 가 있어 요.APPEARANCE_SELECTOR 가 무슨 뜻 이 죠?알림:UIAPPEARANCE_SELECTOR 에 표 시 된 속성 은 외관 에이 전 트 를 통 해 맞 춤 형 제작 을 지원 합 니 다.
예 를 들 어 UIButton 의 contentEdgeInsets 속성 을 설정 하면 바로 호출 할 수 있 습 니 다.
[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIButton 만 들 기:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 50);
[button setTitle:@" UIButton" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];
관찰 할 수 있 도록 button 의 title 을 왼쪽 에 두 는 button 을 만 듭 니 다.UIButton 의 contentEdgeInsets 속성
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;
// default is UIEdgeInsetsZero
contentEdgeInsets 에는 UIButton 의 title 을 가리 키 는 콘 텐 츠 가 있 습 니 다.매개 변수 의미:
위 에서 말씀 드 렸 듯 이 UIEdgeInsets 는 구조 체 유형 입 니 다.안에 네 개의 인자 가 있 는데 그것 이 바로 top,left,bottom,right 이다.이 네 개의 매개 변 수 는 위의 경계,왼쪽 경계,아래 경계,오른쪽 경계 와 의 거 리 를 나타 낸다.
이 네 개의 매개 변수의 값 은 플러스 일 수도 있 고 마이너스 일 수도 있다.왼쪽 예 를 들 면:
left = 10; // , 10
left = -10; // , 10
픽 셀 오른쪽으로 20 개 이동
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
픽 셀 20 개,left=20 을 오른쪽으로 이동 하면 됩 니 다.픽 셀 을 왼쪽으로 20 개 이동
button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
UIButton 의 titleEdgeInsets 속성
title EdgeInsets 와 contentEdgeInsets 의 역할 차이 가 많 지 않 습 니 다.콘 텐 츠 EdgeInsets 를 설정 하고 titleEdgeInsets 를 설정 하면 어떻게 될까요?
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);
효과 보기:UIButton 의 imageEdgeInsets 속성
사진 이 있 는 button 만 들 기:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 200);
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setImage:[UIImage imageNamed:@"test"] forState:UIControlStateNormal];
[self.view addSubview:button];
실행 하기:픽 셀 50 개 오른쪽으로 이동
button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);
효과 보기:픽 셀 50 개 왼쪽으로 이동
button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
효과 보기:여러분 은 스스로 다른 세 개의 파 라 메 터 를 설정 하여 효과 가 어떤 지 보고 스스로 손 을 써 서 이해 하기 편리 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
View의 레이아웃 방법을 AutoLayout에서 따뜻한 손 계산으로 하면 성능이 9.26배로 된 이야기이 기사는 의 15 일째 기사입니다. 어제는 에서 이었습니다. 손 계산을 권하는 의도는 없고, 특수한 상황하에서 계측한 내용입니다 화면 높이의 10 배 정도의 contentView가있는 UIScrollView 레이아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.