iOS 7 -- navigationController 正在设置我的 UIScrollView 的 contentInset 和 ContentOffset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18924431/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:29:12  来源:igfitidea点击:

iOS 7 -- navigationController is setting the contentInset and ContentOffset of my UIScrollView

iosuiscrollviewuinavigationcontrollerios7

提问by William Jockusch

I have a UIScrollView (actually a UICollectionView, but that probably doesn't matter). When it appears in IOS 7, the navigation controller sets its contentInset and contentOffset to values I don't want. It appears to be trying to adjust for the status bar and the navigation bar. I'd greatly prefer it left them alone. I've fixed this by overriding the getter and setter methods for contentInset and contentOffset, with a flag to tell the object whether or not it should accept a set. But is there a better way?

我有一个 UIScrollView(实际上是一个 UICollectionView,但这可能无关紧要)。当它出现在 IOS 7 中时,导航控制器将其 contentInset 和 contentOffset 设置为我不想要的值。它似乎正在尝试调整状态栏和导航栏。我非常希望它让他们独自一人。我通过覆盖 contentInset 和 contentOffset 的 getter 和 setter 方法解决了这个问题,并带有一个标志来告诉对象它是否应该接受一个集合。但是有更好的方法吗?

回答by KDaker

Try setting self.automaticallyAdjustsScrollViewInsets = NOin your main view controller.

尝试self.automaticallyAdjustsScrollViewInsets = NO在主视图控制器中设置。

This was introduced in iOS 7so you might want to wrap that with an iOS version check, if you are supporting iOS 6and below.

这是引入的,iOS 7因此您可能希望使用 iOS 版本检查来包装它,如果您支持iOS 6及以下。



Update

更新

If you are using storyboards, you can do this in the Interface Builder as well as by checking 'Adjust Scroll View Insets'for your selected controller.

如果您使用故事板,您可以在 Interface Builder 中执行此操作,也可以通过检查'Adjust Scroll View Insets'您选择的控制器来执行此操作。

enter image description here

enter image description here

回答by Chuy47

I had a similar problem, after dismissing a viewController, the contentOffset from my tableView was changed to (0, -64).

我遇到了类似的问题,在关闭 viewController 后,tableView 的 contentOffset 更改为 (0, -64)。

my solution was a little weird, I tried all the other answers but had no success, the only thing that fixed my problem was to switch the tableView position in the controls tree of the .xib

我的解决方案有点奇怪,我尝试了所有其他答案但没有成功,解决我的问题的唯一方法是在 .xib 的控件树中切换 tableView 位置

it was the first control in the parent View like this:

它是父视图中的第一个控件,如下所示:

before

before

I moved the tableView right after the ImageView and it worked:

我在 ImageView 之后立即移动了 tableView 并且它起作用了:

after

after

it seems that putting the table view in the first position was causing the trouble, and moving the table view to another position fixed the problem.

似乎将 table view 放在第一个位置引起了麻烦,将 table view 移动到另一个位置可以解决问题。

P.D. I'm not using autoLayout neither storyboards

PD 我没有使用 autoLayout 也没有故事板

hope this can help someone!

希望这可以帮助某人!

回答by amazingthere

I have two solutions:

我有两个解决方案:

1.

1.

self.view = scrollView;

2.

2.

[self.navigationController.toolbar setTranslucent:NO];

回答by Savvy iPhone

I'm having the same problem.

我有同样的问题。

  1. Setting self.automaticallyAdjustsScrollViewInsets = NOsolved the issue for some of the view but not everywhere.

  2. Second solution is to set the content-offset of tableview/view/scrollview in viewWillLayoutSubviews:

    - (void)viewWillLayoutSubviews {
        //Arrange the view
        CGRect tempViewFrame = self.view.frame;
    
        if (tempViewFrame.origin.y == 64.0f) {
            tempViewFrame.origin.y = 0;
            self.view.frame = tempViewFrame;
        }
    }
    
  1. 设置self.automaticallyAdjustsScrollViewInsets = NO解决了某些视图的问题,但并非无处不在。

  2. 第二种解决方案是在viewWillLayoutSubviews中设置tableview/view/scrollview的content-offset:

    - (void)viewWillLayoutSubviews {
        //Arrange the view
        CGRect tempViewFrame = self.view.frame;
    
        if (tempViewFrame.origin.y == 64.0f) {
            tempViewFrame.origin.y = 0;
            self.view.frame = tempViewFrame;
        }
    }
    

回答by Carlos Javier A. Helguero

This fixes the issue on both cases when:

这对解决这两种情况下,当问题:

  1. Showing Status Bar
  2. Showing Status Bar + Navigation Bar

    override func viewDidLayoutSubviews() {
            NSLog("ORIGIN: \(self.view.frame.origin.y)")
    
        if self.view.frame.origin.y == 0 {
            if let rect = self.navigationController?.navigationBar.frame {
                let y = rect.size.height + rect.origin.y
                self.tableView.contentInset = UIEdgeInsetsMake(y, 0, 0, 0)
            }
        } else if self.view.frame.origin.y == 44 || self.view.frame.origin.y == 64 {
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
  1. 显示状态栏
  2. 显示状态栏+导航栏

    override func viewDidLayoutSubviews() {
            NSLog("ORIGIN: \(self.view.frame.origin.y)")
    
        if self.view.frame.origin.y == 0 {
            if let rect = self.navigationController?.navigationBar.frame {
                let y = rect.size.height + rect.origin.y
                self.tableView.contentInset = UIEdgeInsetsMake(y, 0, 0, 0)
            }
        } else if self.view.frame.origin.y == 44 || self.view.frame.origin.y == 64 {
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    

回答by Eric F.

The other answers may work for you, but they didn't work for me. What worked for me was to manually set the yproperty of the contentOffsetin viewDidLayoutSubviewsand viewWillAppear:

其他答案可能对您有用,但对我不起作用。对我有用的是手动设置in和的y属性:contentOffsetviewDidLayoutSubviewsviewWillAppear

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Set the offset to go below the status bar
    collectionView.contentOffset.y = -UIApplication.shared.statusBarFrame.height
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Set the offset to go below the status bar
    collectionView.contentOffset.y = -UIApplication.shared.statusBarFrame.height
}

In my case, my controller had a child collection view which would sometimes get the contentOffsetadjusted and other times not. The way I normalized it was to just manually adjust it every time. I wanted the content to be offset to below the status bar, so I used the status bar height as the value (negative because I want the content to be pushed down).

就我而言,我的控制器有一个子集合视图,有时会得到contentOffset调整,有时不会。我将其标准化的方法是每次都手动调整它。我希望内容偏移到状态栏下方,所以我使用状态栏高度作为值(负数,因为我希望内容被推下)。

回答by Vlad

This is enough

这就够了

- (void)viewDidLoad {

    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;