我有一个UIScrollView(实际上是一个UICollectionView,但这可能并不重要)。 在iOS 7中,当出现导航控制器时,它会设置contentInset和contentOffset的值,而我并不想要这些值。 它似乎试图调整状态栏和导航栏。 我希望它能保持原样。 我通过覆盖contentInset和contentOffset的getter和setter方法,并使用一个标志来告诉对象是否应接受设置来解决了这个问题。 但是有没有更好的方法呢?
尝试在主视图控制器中设置 self.automaticallyAdjustsScrollViewInsets = NO
。
这是在 iOS 7
中引入的,所以如果您支持 iOS 6
及以下版本,则可能需要将其包装在 iOS 版本检查中。
更新
如果您正在使用故事板,则可以通过选中控制器并检查 '调整滚动视图插页'
来在界面构建器中执行此操作。
我也遇到了类似的问题,当我关闭一个viewController后,我的tableView的contentOffset被改变为(0,-64)。
我试过所有其他的答案,但都没有成功,唯一解决我的问题的方法是在.xib文件中切换tableView在控件树中的位置。
之前tableView是父视图中的第一个控件,如下所示:
我将tableView移动到ImageView之后即可,如下所示:
看起来将tableView放在第一位引起了问题,而将tableView移动到其他位置则解决了问题。
P.S. 我没有使用autoLayout和storyboards。
希望这可以帮助到别人!
第一个方案:
self.view = scrollView;
2.
[self.navigationController.toolbar setTranslucent:NO];
NavigationBar
而不是toolbar
,例如navigationController.NavigationBar.Translucent = false;
(注意:Xamarin),这样可以正常工作。 - ta.speot.isSetting self.automaticallyAdjustsScrollViewInsets = NO
solved the issue for some of the view but not everywhere.
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;
}
}
这就足够了
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
viewDidLayoutSubviews
和 viewWillAppear
中的contentOffset
的y
属性: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
}
contentOffset
,有时不会。我规范化的方法是每次手动调整它。我希望内容偏移至状态栏下方,因此我使用状态栏高度作为值(负数,因为我希望内容向下推)。这将在以下两种情况下解决问题:
显示状态栏和导航栏
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)
}
}