gpt4 book ai didi

ios - UIView.transitionWithView 破坏加载器的布局

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:17 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中为 root-view-controller-change 设置动画。交换 View Controller 后,我立即加载第二个 Controller 所需的数据。在加载数据时,我会显示一个加载程序 (MBProgressHUD)。这是我交换 View Controller 的功能:

class ViewUtils {

class func animateRootViewController(duration: NSTimeInterval, changeToViewController: UIViewController) {
let window = UIApplication.sharedApplication().delegate?.window?
if window == nil {
return
}
UIView.transitionWithView(window!,
duration: duration,
options: UIViewAnimationOptions.TransitionFlipFromLeft | UIViewAnimationOptions.AllowAnimatedContent,
animations: {
window!.rootViewController = changeToViewController
},
completion: nil
)
}
}

这一切都很好,但有一件事 - 它完全破坏了加载程序。我附上了正在发生的事情的想象:2nd view controller这是旋转时的第二个 View Controller 。旋转完成后,加载程序看起来很好,微调器和文本补间都在圆角矩形中的正确位置。

我真的不明白为什么会这样,请有人向我解释一下吗?有什么办法可以预防吗?

我显示加载程序的第二个 View Controller 的代码:

override func viewDidLoad() {
super.viewDidLoad()

hud = HUD(containingView: view)
hud.show()

createBackground()
}

还有我的 HUD 类:

class HUD {

private var hudBG: UIView!
private var view: UIView!
private(set) var isShown = false

init(containingView: UIView) {
view = containingView
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func show() {
if !isShown {
if(hudBG == nil) {
hudBG = UIView(frame: CGRectMake(0, 0, view.bounds.width, view.bounds.height))
hudBG.backgroundColor = UIColor(white: 0, alpha: 0.4)
}
view.addSubview(hudBG)
let hud = MBProgressHUD.showHUDAddedTo(view, animated: true)
hud.mode = MBProgressHUDModeIndeterminate
hud.labelText = "Cargando"

hudBG.alpha = 0

UIView.animateWithDuration(0.3, animations: { () -> Void in
self.hudBG.alpha = 1
})
isShown = true
}
}

func hide() {
if isShown {
UIView.animateWithDuration(0.3, animations: {
() -> Void in
self.hudBG.alpha = 0
}, completion: {
(b) -> Void in
self.hudBG.removeFromSuperview()
})
MBProgressHUD.hideHUDForView(view, animated: true)
isShown = false
}
}
}

非常感谢任何想法!

最佳答案

您正在将 HUD 添加到尚未正确初始化的 View 中。如果您从 xib 或 Storyboard加载 View Controller ,则 View 及其 subview 具有从界面加载时的大小。

您必须在将 View 调整为最终大小后添加 HUD。

如果你移动

hud = HUD(containingView: view)
hud.show()

对于 viewDidLayoutSubviews,它应该可以正常工作。

关于ios - UIView.transitionWithView 破坏加载器的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com