如何使用cgrect在父级视图内垂直居中?

发布于 2025-02-07 12:30:17 字数 337 浏览 3 评论 0原文

我试图这样做,但是,当我尝试在模拟器中的其他iPhone模型上运行时,图像的位置会发生变化。我需要用cgrect进行准确做到这一点。提前致谢!

    func configure() {
        //MARK: Logo Image
        view.addSubview(logo)
        logo.image = UIImage(named: "cafelogo")
        logo.layer.cornerRadius = 25
        logo.frame = CGRect(x: 100, y: 200, width: 150, height: 150)

I tried to do this one but, the place of the Image changes when I try to run on a different iPhone model in Simulator. I need to do this exactly with CGRect. Thanks in Advance!

    func configure() {
        //MARK: Logo Image
        view.addSubview(logo)
        logo.image = UIImage(named: "cafelogo")
        logo.layer.cornerRadius = 25
        logo.frame = CGRect(x: 100, y: 200, width: 150, height: 150)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2025-02-14 12:30:17

您应该使用自动限制约束 - 但首先,要向您展示如何使用显式rect /框架来完成此操作:

func configure() {
    //MARK: Logo Image
    view.addSubview(logo)
    logo.image = UIImage(named: "cafelogo")
    logo.layer.cornerRadius = 25
    logo.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
    // center it in the view
    logo.center = view.center
}

但是...您必须从viewDidlayOutsUbviews()< / code>>。否则,View的框架尚未放置:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    configure()
}

请注意,如果view更改的框架 - 例如设备旋转 - 您的徽标图像视图将不再以中心为中心。您可以添加一些新代码以重新中心,但是让自动延迟完成所有工作要容易得多。

您可以在viewDidload()中使用此代码:

    logo.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(logo)
    logo.image = UIImage(named: "cafelogo")
    logo.layer.cornerRadius = 25
    NSLayoutConstraint.activate([
        logo.widthAnchor.constraint(equalToConstant: 150.0),
        logo.heightAnchor.constraint(equalToConstant: 150.0),
        logo.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        logo.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    ])

现在,您的徽标图像视图为150x150,并且始终以view为中心。

You should be using auto-layout constraints -- but first, to show you how to do this with explicit rect / framing:

func configure() {
    //MARK: Logo Image
    view.addSubview(logo)
    logo.image = UIImage(named: "cafelogo")
    logo.layer.cornerRadius = 25
    logo.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
    // center it in the view
    logo.center = view.center
}

but... you have to call this from viewDidLayoutSubviews(). Otherwise, view's frame has not yet been laid-out:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    configure()
}

Note that if the frame of view changes - such as on device rotation - your logo image view will no longer be centered. You could add some new code to re-center it, but it is much easier to let auto-layout do all the work.

You can use this code in viewDidLoad():

    logo.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(logo)
    logo.image = UIImage(named: "cafelogo")
    logo.layer.cornerRadius = 25
    NSLayoutConstraint.activate([
        logo.widthAnchor.constraint(equalToConstant: 150.0),
        logo.heightAnchor.constraint(equalToConstant: 150.0),
        logo.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        logo.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    ])

Now your logo image view is 150x150 and will always remain centered in view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文