我试图在地图应用程序中复制效果,通过触摸地图在顶部和底部栏以及状态栏上再次滑动,即使在 iOS 7 上,当然也在我自己的应用程序中的 iOS 8 上。当然,我在滑动我的工件时没有问题,但是状态栏让我感到困惑,我无法让它在 iOS 8 上滑动,更不用说在 iOS 7 上滑动了。我可以达到的最好方法是通过覆盖prefersStatusBarHidden让它消失;什么当然不适合一般的滑动运动。
怎么可能呢?
我试图在地图应用程序中复制效果,通过触摸地图在顶部和底部栏以及状态栏上再次滑动,即使在 iOS 7 上,当然也在我自己的应用程序中的 iOS 8 上。当然,我在滑动我的工件时没有问题,但是状态栏让我感到困惑,我无法让它在 iOS 8 上滑动,更不用说在 iOS 7 上滑动了。我可以达到的最好方法是通过覆盖prefersStatusBarHidden让它消失;什么当然不适合一般的滑动运动。
怎么可能呢?
你可以这样做:
UIWindow *statusBarWindow = (UIWindow *)[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
if([statusBarWindow isKindOfClass:[UIWindow class]]){
CGRect frame = statusBarWindow.frame;
if(frame.origin.y < 0){
frame.origin.y = 0;
}
else{
frame.origin.y = -20;
}
[UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
statusBarWindow.frame = frame;
} completion:nil];
}
由于 Tommy 的提示,这是我对控件的最终实现:
BOOL controlsWereShown=YES;
-(void)toggleControls{
UIWindow *statusBarWindow = (UIWindow *)[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
CGRect frame=CGRectZero;
if([statusBarWindow isKindOfClass:[UIWindow class]]){
frame= statusBarWindow.frame;
if(frame.origin.y < 0){
frame.origin.y = 0;
}
else{
frame.origin.y = -85;
}
}
[UIView animateWithDuration:.8 animations:^{
self.barConstraint.constant=(controlsWereShown?-85:19);
self.bottomConstraint.constant=(controlsWereShown?50:0);
self.renewalViewConstraint.constant=(controlsWereShown?-120:0);
controlsWereShown=!controlsWereShown;
if (frame.size.width != 0) statusBarWindow.frame = frame;
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate) withObject:nil];
[self.view layoutIfNeeded];
}
completion :^(BOOL finished){
}
];
}
该解决方案中的唯一问题是状态栏与顶部栏一起滑动,而不是在滑动时保持附着在顶部。我通过将结束原点设置为 -85 而不是 -20 来修复它,以便在滑动时跟随杆。