[英]Content of UIScrollView has offset below Navigation Bar
我有一个UIScrollView,其中包含一个UIView,而后者又具有一个UIImageView和几个UIButtons。 我的问题是图像在导航栏下方显示了约20 px。
似乎有类似问题的人有解决方案,例如设置
self.automaticallyAdjustsScrollViewInsets = NO;
或者只是隐藏状态栏
- (BOOL)prefersStatusBarHidden {
return YES;
}
但是,这两种解决方案都不适合我。 第一个将内容设置得太高,并在底部添加了20px的空间。 第二个隐藏状态栏,但偏移量仍然保留。
由于我有Ray Wenderlich编写的本教程中的许多代码,而且我也不知道问题出在我的代码中确切的位置, 因此这是github上代码的链接 。
谁能帮我?
我能够弄清楚发生了什么。 感谢您将链接发布到您的应用。
问题是以下代码:
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
事实证明,首次显示地图时, contentsFrame.size.height
实际上小于bounds.Size.height
。 因此,您的代码将图像垂直居中。 这就是为什么您在顶部看到该偏移量的原因。 精确到17.279像素。
我一直在玩您的应用程序(顺便说一句,这似乎非常有趣),我相信您可以完全摆脱对centerScrollViewContents
的两次调用,它将按预期运行。
如果您还有其他问题,请告诉我。
希望这可以帮助!
正确的解决方案是@LuisCien的解决方案
我在这里补充说,我发现将UIScrollView与MPMediaPickerController一起使用时非常有用,因此:
- (void)fixViewFrameBoundSize {
CGRect contentsFrame = self.view.frame;
CGSize boundsSize = self.view.bounds.size;
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
[self.view setFrame:contentsFrame];
}
和
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[self dismissViewControllerAnimated:YES completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self fixViewFrameBoundSize];
});
}];
}
与所选项目回调相同:
[self dismissViewControllerAnimated:YES
completion:^{
//LP : get item url and play
dispatch_async(dispatch_get_main_queue(), ^{
[self fixViewFrameBoundSize];
});
MPMediaItem *item = [collection representativeItem];
当您具有带有UIView的XIB并按如下所示在UISCrollView中对其进行转换时,此解决方案效果很好:
- (void)scrollViewMakeScrollable {
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+44.0);
[((UIScrollView*)self.view) setContentSize:scrollableSize];
[((UIScrollView*)self.view) setAlwaysBounceHorizontal:NO];
[((UIScrollView*)self.view) setAlwaysBounceVertical:NO];
self.automaticallyAdjustsScrollViewInsets = NO;
}
所以通常当你做像
- (void) viewDidLoad {
[super viewDidLoad];
[self scrollViewMakeScrollable];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.