iOS setContentOffset 在 ipad 上不起作用

iOS setContentOffset not working on ipad(iOS setContentOffset 在 ipad 上不起作用)
本文介绍了iOS setContentOffset 在 ipad 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这真的很奇怪.我有一个包含三个图像的滚动视图,用户滑动查看下一个图像.但是,我希望第一个屏幕从中间图像开始.简单;我会使用 setContentOffset 一切都会好的.

This is really strange. I have a scrollview that contains a three images, and the user swipes to see the next image. However, I want the first screen to start at the middle image. Easy; I'll use setContentOffset and all will be fine.

代码适用于 iPhone 模拟器,但不适用于 iPad 模拟器(或设备!)

The code works on the iPhone simulator, but not on the iPad simulator (or device!)

CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat height = CGRectGetHeight(screen);
CGFloat width = CGRectGetWidth(screen);

CGPoint rightOffset = CGPointMake(width, 0);
[scrollView setContentOffset:rightOffset animated:YES];

在添加滚动视图之前设置(实际上我们可以在添加滚动视图之后执行此操作,结果相同).

All set just before the scrollView is added (and in fact we can do this after the scrollview is added with the same result).

宽度在 ipad 上返回 768,在 iphone 上返回 320.

width is returning 768 on ipad and 320 on the iphone.

这是一个错误吗?Xcode 4.4.1 和 ios 6.

Is this a bug? Xcode 4.4.1 and ios 6.

看起来这与创建顺序有关;移至 viewWillAppear 而不是 viewDidLoad 并且显然在 iphone 和 ipad 上工作.只是前后不一致就很让人吃惊了....

Looks like this is to do with creation order; moved to viewWillAppear rather than viewDidLoad and apparently working on iphone and ipad. Just the inconsistency is very surprising....

推荐答案

你不应该在viewDidLoad中初始化UI几何相关的东西,因为此时你的view的几何没有设置,结果会不可预知.

You should not initialise UI geometry-related things in viewDidLoad, because the geometry of your view is not set at this point and the results will be unpredictable.

正如您所发现的,viewWillAppear 是执行这些操作的正确位置,此时几何已设置好,因此获取和设置与几何相关的属性是有意义的.

As you have discovered, viewWillAppear is the correct place to do these things, at this point the geometry is set so getting and setting geometry-related properties makes sense.

viewDidAppear.

viewDidAppear.

更新 (ios6)

在 ios6 中使用自动布局时,在布局子视图之前不会设置框架.在这些条件下获取帧数据的正确位置是在 viewController 方法 -viewDidLayoutSubviews 中.

When using autolayout in ios6, frames are not set until subviews have been laid out. The right place to get frame data under these conditions is in the viewController method -viewDidLayoutSubviews.

这在 -viewWillAppear 之后被称为.但请注意 - 他们并不总是被召集在一起.例如,旋转后调用 -viewWDidLayoutSubviews,而不调用 -viewWillAppear.每次视图变为可见视图*时都会调用 -viewWillAppear,-viewDidLayoutSubviews 不一定(仅当我们的视图需要中继时).

This is called after -viewWillAppear. But take care - they are not always called together. For example, -viewWDidLayoutSubviews is called after a rotation, -viewWillAppear is not. -viewWillAppear is called every time a view becomes the visible view*, -viewDidLayoutSubviews not necessarily (only if us views needed relaying out).

(* 除非应用程序是后台程序并且正在成为前台应用程序,否则 -viewWillAppear 不会被调用)

(* unless the app was backgrounder and is becoming foreground app, then -viewWillAppear is not called)

这篇关于iOS setContentOffset 在 ipad 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

iOS refresh annotations on mapview(iOS在mapview上刷新注释)
How to calculate distance from user location to annotation when user moves(用户移动时如何计算用户位置到注释的距离)
In which case that mapView:viewForAnnotation: will be called?(在这种情况下 mapView:viewForAnnotation: 会被调用吗?)
NSTimeInterval to unix timestamp(NSTimeInterval 到 unix 时间戳)
How do I get a server timestamp from Firebase#39;s iOS API?(如何从 Firebase 的 iOS API 获取服务器时间戳?)
iOS Swift - Get the Current Local Time and Date Timestamp(iOS Swift - 获取当前本地时间和日期时间戳)