UINavigationController 子视图不滚动
UINavigationController Subview not Scrolling
我有一个导航控制器和一个视图,我附加到导航控制器的子视图,我使用下面的代码在向上滑动时隐藏导航栏
[self.navigationController.view addSubview: categoryView];
self.navigationController.hidesBarsOnSwipe = YES;
它在向上滑动 tableview 时隐藏到顶部,但我希望子视图应该与导航控制器一起向上移动并且它应该适合顶部,而向下滑动导航栏应该与子视图一起下降.
我试过了
[self.navigationController.navigationBar addSubview: categoryView];
在此导航栏和子视图都隐藏到顶部。我想为此会有一些简单的解决方案,我搜索了很多但没有找到完美的解决方案。我也尝试了一些库,它们不适合 iPhone X 并且在 iOS 11.
中有一些问题
更新:为问题添加图片before scrolling
和
after scrolling the tableview
将这段代码写在viewDidLoad
[self.navigationController setNavigationBarHidden:YES];
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
[self.navigationController setNavigationBarHidden:NO];
} else {
[self.navigationController setNavigationBarHidden:YES];
}
}
试试这个。
使用自定义视图作为 header 视图而不是使用导航栏(隐藏导航栏)。现在为您的 header 视图提供约束(高度约束)。当您滚动 table 视图时,将调用方法 -scrollViewDidScroll
。检查滚动视图的内容偏移量,如果它大于 0。
headerView.heightconstraint -= scrollViewContentoffset.y;
已编辑:
您可以将子视图添加到具有安全区域约束的视图中。
F.e。以编程方式:
self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[self.headerView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:0],
[self.headerView.bottomAnchor constraintEqualToAnchor:guide.topAnchor constant:64],
[self.headerView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:0],
[self.headerView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:0]
]];
[NSLayoutConstraint activateConstraints:@[
[self.tableView.topAnchor constraintEqualToAnchor:self.headerView.bottomAnchor constant:0],
[self.tableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:0],
[self.tableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:0],
[self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:0]
]];
}
要 show/hide 滚动条上的导航栏,您可以使用:
if (scrollView.contentOffset.y <= 0) {
[self.navigationController setNavigationBarHidden:NO animated: YES];
} else {
[self.navigationController setNavigationBarHidden:YES animated: YES];
}
[UIView animateWithDuration:[CATransaction animationDuration]
animations:^{
[self.view layoutIfNeeded];
}];
在 LetsBuildThatApp Youtube 频道上关注 Brian Voong 的精彩教程。他用大量的细节解释了您所指的相同功能。
希望这对您有所帮助..
我有一个导航控制器和一个视图,我附加到导航控制器的子视图,我使用下面的代码在向上滑动时隐藏导航栏
[self.navigationController.view addSubview: categoryView];
self.navigationController.hidesBarsOnSwipe = YES;
它在向上滑动 tableview 时隐藏到顶部,但我希望子视图应该与导航控制器一起向上移动并且它应该适合顶部,而向下滑动导航栏应该与子视图一起下降.
我试过了
[self.navigationController.navigationBar addSubview: categoryView];
在此导航栏和子视图都隐藏到顶部。我想为此会有一些简单的解决方案,我搜索了很多但没有找到完美的解决方案。我也尝试了一些库,它们不适合 iPhone X 并且在 iOS 11.
中有一些问题更新:为问题添加图片before scrolling 和 after scrolling the tableview
将这段代码写在viewDidLoad
[self.navigationController setNavigationBarHidden:YES];
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
[self.navigationController setNavigationBarHidden:NO];
} else {
[self.navigationController setNavigationBarHidden:YES];
}
}
试试这个。
使用自定义视图作为 header 视图而不是使用导航栏(隐藏导航栏)。现在为您的 header 视图提供约束(高度约束)。当您滚动 table 视图时,将调用方法 -scrollViewDidScroll
。检查滚动视图的内容偏移量,如果它大于 0。
headerView.heightconstraint -= scrollViewContentoffset.y;
已编辑:
您可以将子视图添加到具有安全区域约束的视图中。
F.e。以编程方式:
self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[self.headerView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:0],
[self.headerView.bottomAnchor constraintEqualToAnchor:guide.topAnchor constant:64],
[self.headerView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:0],
[self.headerView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:0]
]];
[NSLayoutConstraint activateConstraints:@[
[self.tableView.topAnchor constraintEqualToAnchor:self.headerView.bottomAnchor constant:0],
[self.tableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:0],
[self.tableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:0],
[self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:0]
]];
}
要 show/hide 滚动条上的导航栏,您可以使用:
if (scrollView.contentOffset.y <= 0) {
[self.navigationController setNavigationBarHidden:NO animated: YES];
} else {
[self.navigationController setNavigationBarHidden:YES animated: YES];
}
[UIView animateWithDuration:[CATransaction animationDuration]
animations:^{
[self.view layoutIfNeeded];
}];
在 LetsBuildThatApp Youtube 频道上关注 Brian Voong 的精彩教程。他用大量的细节解释了您所指的相同功能。
希望这对您有所帮助..