将cgrect框架上的y值更改为屏幕顶部下方的80

问题描述

我希望我的快速代码在屏幕上放置2个图像视图。图像视图通过cgrect框架放置。我想要的是box1可以从屏幕顶部覆盖到屏幕高度的80%。底部的20%高度应由box2覆盖。在y值上,我需要box2从屏幕顶部下方的80开始。

import UIKit

class ViewController: UIViewController {
    
    
    
    var box1 = UIImageView()
    var box2 = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let VCframe = self.view.frame
        let height = VCframe.height * 0.8
        
        let height2 = VCframe.height * 0.2
        let widthx = VCframe.width
        view.addSubview(box1)
        view.addSubview(box2)
        box1.backgroundColor = .red
        box2.backgroundColor = .blue
        
        
        box1.frame = CGRect(x: 0,y: 0,width: widthx,height: height)
        box2.frame = CGRect(x: 0,height: height2)
        
    }
    
    
    
    
}

解决方法

所以我认为这是您正在寻找的解决方案:

    class ViewController: UIViewController {

    var box1 = UIImageView()
    var box2 = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let VCframe = self.view.frame
        let height = VCframe.height * 0.8
        
        let height2 = VCframe.height * 0.2
        let widthx = VCframe.width
        view.addSubview(box1)
        view.addSubview(box2)
        box1.backgroundColor = .red
        box2.backgroundColor = .blue
        box1.translatesAutoresizingMaskIntoConstraints = false
        box2.translatesAutoresizingMaskIntoConstraints = false
        
        
        box1.frame = CGRect(x: 0,y: 0,width: widthx,height: height)
        box2.frame = CGRect(x: 0,y: height,height: height2)
        
    }
}

但是,将框架用作布局模式并不是一个好主意。

这可能是您想要做的:

    class ViewController: UIViewController {

    var box1 = UIImageView()
    var box2 = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadViews()
    }
    
    private func loadViews() {
        view.addSubview(box1)
        view.addSubview(box2)
        box1.backgroundColor = .red
        box2.backgroundColor = .blue
        box1.translatesAutoresizingMaskIntoConstraints = false
        box2.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            box1.leadingAnchor.constraint(equalTo: view.leadingAnchor),box1.trailingAnchor.constraint(equalTo: view.trailingAnchor),box1.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height*0.8),box1.topAnchor.constraint(equalTo: view.topAnchor),box2.leadingAnchor.constraint(equalTo: view.leadingAnchor),box2.trailingAnchor.constraint(equalTo: view.trailingAnchor),box2.topAnchor.constraint(equalTo: box1.bottomAnchor),box2.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...