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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기