objective-c 为什么 UIScrollView 在 ios 6 和 ios 7 中从顶部留出空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20630059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why UIScrollView is leaving space from top in ios 6 and ios 7
提问by bhavya kothari




I have turned off Autolayout and viewcontroller is embedded in navigation controller. I am using Xcode 5, Storyboard.
我已经关闭了 Autolayout 并且 viewcontroller 嵌入在导航控制器中。我正在使用 Xcode 5,故事板。
I don't understand why is it leaving space from top. Actually in storyboard i have put my label exactly below navigation bar. But when i run it on simulator then it leaves space from top.
我不明白为什么它会从顶部留出空间。实际上,在情节提要中,我已将标签正好放在导航栏下方。但是当我在模拟器上运行它时,它会从顶部留下空间。
One of the hacky way to resolve this
解决此问题的一种骇人听闻的方法
-(void)viewWillLayoutSubviews
{
self.scrollViewMain.frame = CGRectMake(0, -70, 320, 800);
self.scrollViewMain.contentSize = CGSizeMake(320, 800);
}
But am i missing something very simple approach.
但是我错过了一些非常简单的方法。
回答by Raheel Sadiq
In Xcode 5, in storyboard select your controller > in Attribute Inspector disable Adjust Scroll View Insets Also check if you have set any contentInset
在 Xcode 5 中,在情节提要中选择您的控制器 > 在属性检查器中禁用调整滚动视图插入还要检查您是否设置了任何 contentInset
Edit: I attached a pic
编辑:我附上了一张照片


回答by Anooj VM
The fix is to implement the following line of code after in the viewDidLoad;
解决方法是在 viewDidLoad 之后实现以下代码行;
self.automaticallyAdjustsScrollViewInsets = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
回答by chengsam
Update for iOS 11
iOS 11 更新
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
回答by Svitlana
回答by Ravi Kumar
Go to main storyboard. Select view controller, see property "Adjust Scroll view Insects" in attribute inspector. if you do not want top spacing.then uncheck "Under Top Bars". if you don not want bottom spacing.then uncheck "Under Bottom Bars". this kind of problem come when we use scroll view related controllers.
This is a feature for navigation and tab bar to adjust content in scroll view like controls.
转到主故事板。选择视图控制器,在属性检查器中查看属性“调整滚动视图昆虫”。如果您不想要顶部间距。然后取消选中“在顶部栏下”。如果您不想要底部间距。然后取消选中“在底部栏下”。当我们使用滚动视图相关的控制器时会出现这种问题。
这是导航和标签栏的一项功能,可以像控件一样调整滚动视图中的内容。
回答by webjunkie
For me in xcode 7 and Swift 2.x I was populating a UIScrollView with a for loop and had to set the contentInset to zero.
对于我在 xcode 7 和 Swift 2.x 中,我使用 for 循环填充 UIScrollView 并且必须将 contentInset 设置为零。
for image in images {
self.scrollView.contentInset = UIEdgeInsetsZero
}

