ios - contentOffset 和 contentInset 如何在 iOS 7 中为 UIScrollView 工作?

标签 ios uiscrollview contentoffset

这是我对 UIScrollView (iOS 7) 的理解:

  • contentOffset 定义内容 View 的原点
  • contentInset 可以有效地在内容 View 周围添加“填充”

在 Xcode 中,我创建了一个单 View 应用程序。在 IB 中,我向 View Controller 添加了一个 UIScrollView,并将 Controller 嵌入到导航 Controller 中。在 View Controller 代码中,我添加了以下方法:

- (void)viewDidAppear:(BOOL)animated
{
    [self.scrollView setContentSize:CGSizeMake(380, 1000)];
    NSLog(@"top = %f, bounds top %f", self.scrollView.frame.origin.y, self.scrollView.bounds.origin.y);
    NSLog(@"offset y = %f", self.scrollView.contentOffset.y);
    NSLog(@"height = %f", self.scrollView.contentSize.height);
    NSLog(@"inset top = %f", self.scrollView.contentInset.top);
    NSLog(@"inset bottom = %f", self.scrollView.contentInset.bottom);
}

我的结果:

top = 0.000000, bounds top -64.000000
offset y = -64.000000
height = 1000.000000
top = 64.000000
bottom = 0.000000

contentInset.top 值有效地将内容向下移动,使其位于导航栏下方。为什么 contentOffset.y 设置为 -64,而 ScrollView 的边界设置为 -64?

提前致谢

最佳答案

因为 automaticallyAdjustsScrollViewInsets 默认值在 iOS 7.0 中是 YES

The default value of this property is YES, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to NO if your view controller implementation manages its own scroll view inset adjustments.

UIViewController Class Reference

关于ios - contentOffset 和 contentInset 如何在 iOS 7 中为 UIScrollView 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435254/

相关文章:

ios - 从呈现的 View Controller 返回时保留 UICollectionView contentOffset

objective-c - 保留计数总是-1?

iOS 8 MKAnnotationView rightCalloutAccessoryView 错位

ios - 基于 UIScrollView 的 contentOffset 改变颜色

ios - 如何在 cellForRowAtIndexPath 方法中判断滚动方向(右上左下)

ios - 每次重新加载 tableview 部分时,UITableView 都不会向上滚动

ios - 如何实时获取UITableView的滚动位置

ios - 无法在 segue 期间传递唯一数据

UIScrollView中的iOS UITextField没有响应

iphone - UIScrollView 每次访问 View 都会更改滚动位置