iOS的原点不是(0,0)

时间:2019-07-01 17:23:01

标签: ios swift uiview uiviewcontroller

我正在使用swift 4.2编写iOS应用程序。我想将自定义视图添加为位于中心的ViewController视图的子视图。我在viewDidAppear函数中设置自定义视图的框架。在此功能中,视图的原点是“错误的”-(64; 0)而不是(0; 0)。

这是我的代码:

private var blurEffectView: UIVisualEffectView!
private var profileInputView: ProfileInputView!

override func viewDidLoad() {
    profileInputView = ProfileInputView.init()
    profileInputView.setDelegate(profileInputViewDelegate: self)
    blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.prominent))
    setupViewsFinished = true
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if !setupViewsFinished { return }
    blurEffectView.frame = view.frame
    blurEffectView.center = view.center
    profileInputView.frame = blurEffectView.frame
    profileInputView.frame.size = CGSize.init(width: blurEffectView.frame.width * 0.8, height: blurEffectView.frame.height * 0.8)
    profileInputView.center = blurEffectView.center
}

func showInputView(with description: String) {
    profileInputView.configure(with: description)
    blurEffectView.contentView.addSubview(profileInputView)
    view.addSubview(blurEffectView)
}

func hideInputView(with description: String) {
    profileInputView.removeFromSuperview()
    blurEffectView.removeFromSuperview()
}

我在viewDidAppear中放置了一个调试点,并打印了视图框架的原点:

po view.frame.origin

结果: (0.0,64.0)   -x:0.0   -y:64.0

我想将自定义视图放在一个中心,但是它位于下面,并且下部不可见。

我在做什么错了?

PS:不使用UIVisualEffectView,结果是相同的

1 个答案:

答案 0 :(得分:-1)

import UIKit

class ViewController: UIViewController {

    private var blurEffectView: UIVisualEffectView!
    private var profileInputView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)

        blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.prominent))
        profileInputView = UIView()
        profileInputView.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)

        view.addSubview(blurEffectView)
        blurEffectView.contentView.addSubview(profileInputView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        blurEffectView.frame = view.frame
        blurEffectView.center = view.center

        profileInputView.frame = blurEffectView.bounds
        profileInputView.bounds.size = CGSize.init(width: blurEffectView.bounds.width * 0.8, height: blurEffectView.bounds.height * 0.8)
        profileInputView.center = blurEffectView.center
    }

}

嘿,伙计。我不确定这正是您想要的。但这是居中的