我希望在我在iPhone上向下滚动时隐藏两个栏。当我向上滚动时,它们应该再次出现。我该如何处理?
我希望在我在iPhone上向下滚动时隐藏两个栏。当我向上滚动时,它们应该再次出现。我该如何处理?
- (void)scrollViewWillBeginScroll :(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < lastOffset.y) {
[toolBar setHidden:YES];
[[[self navigationController] navigationBar] setHidden:YES];
} else{
// unhide
}
}
- (void)scrollViewDidScroll :(UIScrollView *)scrollView {
/// blah blah
lastOffset = scrollView.contentOffset;
}
scrollViewWillBeginScroll
方法。 - Shmidt对于我来说,被接受的答案并不适用,因为 scrollViewWillBeginScroll:
不是一个委托方法。
相反,我执行以下操作
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldHide" object:self];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
if(!decelerate)
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide"
object:self];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide"
object:self];
}
应用程序中的任何对象都可以监听此通知,例如:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldHide"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
//hide tab bar with animation;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldUnhide"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
//Unhide tab bar with animation;
}];
}
这段代码将隐藏任何滚动条。如果你只想有一个向下的滚动条,那么可以使用与接受的答案中相同的locationOffset
技巧。
你可以检查一下,它可用于iOS8,我认为这是您要寻找的相反的东西...但是值得检查,因为它是标准的,并且这就是Safari的工作方式。
Swift
var hidesBarsOnSwipe: Bool
Objective-C
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe Discussion
当将此属性设置为YES时,向上滑动将隐藏导航栏和工具栏。向下滑动会再次显示两个栏。如果工具栏没有任何项,则在滑动后仍然可见。此属性的默认值为NO。
这是我的Swift解决方案,它运行得非常好
func scrollViewDidScroll(scrollView: UIScrollView) {
let navController: UINavigationController = self.navigationController!
if self.collectionView.panGestureRecognizer.translationInView(self.view).y <= 0.0 {
defaultCenter.postNotificationName("stuffShouldHide", object: self)
} else {
defaultCenter.postNotificationName("stuffShouldUnhide", object: self)
}
}
scrollViewDidScroll:
中使用setToolbarHidden:animated:
和setNavigationBarHidden:animated:
当contentOffset
的 x 值增加时吗? - dasdom