我有一个 UIStackView,它包含一个 ImageView 和一个 TextView 。 ImageView 有一个事件指示器 View 作为 subview (我以编程方式添加它)。当 View 最初加载时,我将事件指示器 View 置于 ImageView 的中心并启动 AIV 的动画。但是,在等待图像加载的过程中,如果我旋转 ImageView ,AIV 需要重新居中。我添加了代码来监听状态栏方向更改通知,并有一个根据通知调用的方法 (UIApplicationDidChangeStatusBarOrientationNotification)。在该方法中,我尝试获取 ImageView 的边界,并使用它来将 AIV 在 ImageView 中重新居中。但是,当设备方向改变时, ImageView 的边界值似乎没有改变。设备方向更改后是否有一种“可靠”的方法来获取 View 的更新边界?代码 ->
// start listening for orientation changes
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationDidChange:",
name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
然后我有:
func orientationDidChange(notification: NSNotification) {
// get image view bounds
let viewBounds = _imageView.bounds
// recenter the activity indicator
_activityIndicator.center = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds))
}
更新:
所以我摆脱了上面的内容并尝试了这个:
// setup activity indicator
_activityIndicatorView.translatesAutoresizingMaskIntoConstraints = true
_imageView.addSubview(_activityIndicatorView)
// set constraints
let centerXConstraint = NSLayoutConstraint(item: _activityIndicatorView, attribute: .CenterX,
relatedBy: .Equal, toItem: _imageView,
attribute: .CenterX, multiplier: 1.0,
constant: 0.0)
_activityIndicatorView.addConstraint(centerXConstraint)
let centerYConstraint = NSLayoutConstraint(item: _activityIndicatorView, attribute: .CenterY,
relatedBy: .Equal, toItem: _imageView,
attribute: .CenterY, multiplier: 1.0,
constant: 0.0)
_activityIndicatorView.addConstraint(centerYConstraint)
在我看来,这确实是加载方法。控制台输出如下:
2015-10-27 19:26:09.303 Historic Sites Navigator[373:93902] View 层次结构未准备好约束: 添加到 View 时,约束的项目必须是该 View (或 View 本身)的后代。如果在组装 View 层次结构之前需要解决约束,这将会崩溃。中断 -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] 进行调试。 2015-10-27 19:26:09.308 历史遗址导航器[373:93902] View 层次结构未准备好约束。 约束: 容器层次结构: > | > 在容器层次结构中找不到 View :> 该 View 的 super View :> 2015-10-27 19:26:09.310 Historic Sites Navigator[373:93902] * 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法在 View 上安装约束。约束是否引用 View 子树外部的某些内容?这是非法的。约束: View :>' * 首先抛出调用堆栈: (0x2643767b 0x377b2e17 0x264375c1 0x27177983 0x2a628f85 0x2a628dd5 0x2a628cef 0x2ad33971 0x2a628bd7 0xdf6c4 0xdfe34 0x2a5271c7 0x2a526dad 0x2ad8ca09 0x2a8689ad 0x2a88c095 0x2a88e6c5 0x2a88e931 0x2a61e989 0x2a891bb7 0x2ab33085 0x2ac09b39 0x2ac09967 0x2a883d9 5 0xccac8 0xccc10 0x2a659487 0x2a71440b 0x2a7cc0b9 0x2a7d79eb 0x2a519ecd 0x263fa4c9 0x263f87cd 0x263f8bff 0x2634c119 0x2634bf05 0x2f 502ac9 0x2a58ef15 0xd6e74 0x37f21873) libc++abi.dylib:以 NSException 类型的未捕获异常终止
最佳答案
对于 iOS 8 和 9,在 View 大小更改时调用以下函数(例如在界面旋转时):
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (coordinator) -> Void in
// here the new size is already set, so place your code here
}, completion: nil)
}
除此之外,您还可以使用约束(自动布局)来定位 UIActivityIndicatorView。
您当前的自动布局错误是因为您必须:
- 将 TranslatesAutoresizingMaskIntoConstraints 设置为 false
- 将约束添加到 ImageView ,而不是 ai-view
关于ios - 如何解决 View 居中之谜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329806/