iOS 11 UINavigation Item 좌우 간극 제거 방법
2647 단어 iOS11UINavigationItem
iOS 11 버 전 은 Nav 계층 구조의 변화 로 인해 예전 의 방법 이 이상 적 인 이동 효 과 를 얻 지 못 해 상단 의 단 추 를 완전히 왼쪽 또는 오른쪽 에 두 었 습 니 다.
사고의 방향 을 수정 하 다.
iOS 11 이전에 기 존 방식 으로 설정 하고 iOS 11 이후 추가 적 인 간격 제약 수정 을 통 해 이동 효 과 를 얻 을 수 있 습 니 다.
view Debug 의 인터페이스 에서 관찰 하면 UIButton BarStackView 의 왼쪽 과 오른쪽 16 의 제약 을 0 으로 바 꾸 면 됩 니 다.
핵심 코드
탐색 기 view 코드 설정
//0:leftBarButtonItems,1:rightBarButtonItems
- (void)initBarItem:(UIView*)view withType:(int)type{
UIBarButtonItem * buttonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
// .iOS 11
UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = -16;//
switch (type) {
case 0:
if (!IS_IOS_VERSION_11) {
self.navigationItem.leftBarButtonItems =@[spaceItem,buttonItem];
}else{
self.navigationItem.leftBarButtonItems =@[buttonItem];
}
break;
case 1:
if (!IS_IOS_VERSION_11) {
self.navigationItem.rightBarButtonItems =@[spaceItem,buttonItem];
}else{
self.navigationItem.rightBarButtonItems =@[buttonItem];
}
break;
default:
break;
}
}
iOS 11 상황 에서 의 오프셋 문 제 를 처리 하고 변 거 리 를 16 으로 제약 하 는 값 을 0 으로 바 꿉 니 다.
-(void)viewDidLayoutSubviews{
if (!IS_IOS_VERSION_11) return;
UINavigationItem * item=self.navigationItem;
NSArray * array=item.leftBarButtonItems;
if (array&&array.count!=0){
// , leftBarButtonItem customeView , UIBarButtonSystemItemFixedSpace item
UIBarButtonItem * buttonItem=array[0];
UIView * view =[[[buttonItem.customView superview] superview] superview];
NSArray * arrayConstraint=view.constraints;
for (NSLayoutConstraint * constant in arrayConstraint) {
if (fabs(constant.constant)==16) {
constant.constant=0;
}
}
}
}
변경 후 효과.png
데모 주소:https://github.com/StoneMover/navDemo.git
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
iOS 11 WKWebView 에서 콘 텐 츠 를 불 러 올 수 없 는 해결 방법문제 설명:iOS 9 와 iOS 10 은 WKWebView 로 URL 을 불 러 오 는 데 문제 가 없습니다.iOS 11 은 공백 입 니 다. NSMutableURL Request 를 사 용 했 는 지 iOS 11 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.