Programing

UIViewController가 UINavigationBar 아래에서 확장되는 반면 UITableViewController는 확장되지 않는 이유는 무엇입니까?

lottogame 2020. 11. 29. 09:32
반응형

UIViewController가 UINavigationBar 아래에서 확장되는 반면 UITableViewController는 확장되지 않는 이유는 무엇입니까?


나는이 UITabbarControllerUINavigationController그것입니다. 나는의 서브 클래스가 UIView나는대로 할당하는 것이 viewUIViewController의를 navController. 이것은 꽤 표준적인 것입니다. 그렇죠? 이것이 내가하는 방법

_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame];
self.view = _productCategoryView;

이것은 viewA가 들어 UITableView으로subView

_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_productCategoryTableView.backgroundColor = [UIColor clearColor];
[self addSubview:_productCategoryTableView];

디버깅 self.backgroundColor = [UIColor blueColor]을 위해 뷰를 설정 하고 있습니다.

위의 초기화 tableView에서 뷰와 테이블 frame이 동일 하다고 생각할 수 있습니다 . 그러나에서 실행 iOS 7하면 뷰의 원점이 UINavigationBar. self.navigationBar.translucent = YES;내 하위 클래스에서 설정하고 있기 때문에 이해할 수 UINavigationController있습니다. 그러나 내가 이해하지 못하는 것은 테이블이 어떻게 바로 아래에 앉아 있는지입니다 navBar. 그것은 또한 (0, 0)뒤에서 시작해야하지 navBar않습니까? 아래 스크린 샷을 참조하십시오 Scenario 1. 뒤에 푸른 색조를 주목하십시오navBar

시나리오 1

지금, 나는 push또 다른 viewController탐색 스택에, 단순히 의해 사용 [self.navigationController pushViewController.....]. 다시 나는 그것에 있는 관습 UIViewtableView있다. 그러나 나는 또한 UILabel이 테이블 위에 있으며 디버깅을 위해 다시 redColor. 이번에는 레이블 origin을 뷰의 레이블 과 거의 동일하게 설정합니다.

CGRect boundsInset = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(10, 10, 10, 10));

CGSize textSize = [_titleLabel.text sizeWithFont:_titleLabel.font
                               constrainedToSize:CGSizeMake(boundsInset.size.width, MAXFLOAT)
                                   lineBreakMode:NSLineBreakByWordWrapping];
printSize(textSize);
_titleLabel.frame = CGRectMake(boundsInset.origin.x,
                               boundsInset.origin.y,
                               boundsInset.size.width,
                               textSize.height);

그래서 위의 논리에 따라 레이블이 표시되어야합니다. 그러나 이번에는 그렇지 않습니다. 이번에는 레이블이 navBar.

시나리오-2

navBar 뒤에있는 붉은 색조에 주목하십시오.

navBar 아래에 subView를 일관되게 정렬하고 싶습니다. 내 질문은

1. How is the tableView offset by 64pixels (height of nav + status bar in iOS 7) automatically, even though it's frame is same as the view's?

2. Why does that not happen in the second view?


기본적으로 UITableViewController의 뷰는 iOS7에서 자동으로 삽입되므로 탐색 모음 / 상태 표시 줄 아래에서 시작되지 않습니다. 이것은 Interface Builder에서 UITableViewController의 Attributes Inspector 탭에있는 "Adjust scroll view insets"설정 또는 setAutomaticallyAdjustsScrollViewInsets:UIViewController 메소드에 의한 컨트롤러 입니다.

UIViewController의 내용에 대해 뷰의 내용이 위쪽 / 아래쪽 막대 아래로 확장되지 않도록하려면 Interface Builder에서 위쪽 막대 / 아래쪽 막대 아래 가장자리 확장 설정을 사용할 수 있습니다. edgesForExtendedLayout숙소 를 통해 출입하실 수 있습니다.


목표 -C :

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
 }

스위프트 2 :

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3+ :

self.edgesForExtendedLayout = []

@Gank의 대답은 정확하지만이를 수행하는 가장 좋은 장소는 UINavigationControllerDelegate(있는 경우)입니다.

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    viewController.edgesForExtendedLayout = UIRectEdge.None
}

참고 URL : https://stackoverflow.com/questions/19143353/why-does-uiviewcontroller-extend-under-uinavigationbar-while-uitableviewcontrol

반응형