ios - 如何在 swift ios 中动态获取屏幕元素的大小?

标签 ios swift screen frame

抱歉,如果我的问题相当愚蠢:我目前正在处理屏幕元素,需要根据父 View 的大小动态获取它们的大小。

如果我使用框架,我需要获得固定尺寸,对吧?例如:

let object = UIView(frame: CGRect(x: 20 + ( (10+34)*nrX ), 
  y: 10+(10+34)*nrY, width: 34, height: 34))

当然,我可以手动调整大小,但它们保持固定。

是否有可能动态获取元素的大小和元素之间的距离?例如:每个元素的大小可以是父 View 长度的 1/10 和宽度的 1/10 吗?

这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var testLabel: UILabel!

let testSubView = UIView()
var allObjects = [UIView]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    createTestSubview()

    testLabel.text = "Elements: \(allObjects.count)"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func createTestSubview(){


        // Creating testSubView

        testSubView.backgroundColor = UIColor.purple //For test purpose

        testSubView.layer.borderWidth = 0

        testSubView.layer.cornerRadius = 5
        testSubView.layer.masksToBounds = true

        testSubView.translatesAutoresizingMaskIntoConstraints = false //Don't forget this line
        view.addSubview(testSubView)



        let horizontalConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
        view.addConstraint(horizontalConstraint)

        let verticalConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
        view.addConstraint(verticalConstraint)

        let widthConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.width, multiplier: 0.7, constant: 0)
        view.addConstraint(widthConstraint)

        let heightConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.height, multiplier: 0.7, constant: 0)
        view.addConstraint(heightConstraint)

    createScreen()

}


func createObject(nrX:Int,nrY:Int,tagNr:Int) {



    let object = UIView(frame: CGRect(x: 10 + ( (10+20)*nrX ), y: 10+(10+20)*nrY, width: 20, height: 20))
    object.backgroundColor = UIColor.yellow //For test purpose

    object.tag = tagNr

    object.translatesAutoresizingMaskIntoConstraints = false
    testSubView.addSubview(object)
    allObjects.append(object)

}

func createScreen(){

    var count = 0
    let colum = 7

    let rows  = 7
    for j in 0..<rows{
        for i in 0..<colum{
            if count < 49 {
                createObject(nrX: i,nrY:j,tagNr:count)
                count += 1
            }
        }
    }

}

最佳答案

您可以通过以下方式获取主视图的大小:

let mainViewHeight = UIScreen.main.nativeBounds.height
let mainViewWidth = UIScreen.main.nativeBounds.width

关于ios - 如何在 swift ios 中动态获取屏幕元素的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918436/

相关文章:

ios - NSString中的指数

python - 这是什么时间格式? 20151104101700-0800

ios - 使用 Firebase 的本地通知

android - UNITY 触摸位置到世界位置 2D

android - android中的屏幕优化

ios - Xcode 6 中与 iPhone 5/6/6+ 屏幕分辨率相关的相同按钮位置

ios - 如何让 UISearchDisplayController 仅在按下搜索按钮后进行搜索

swift - FIREBASE 4 类型 'StorageReference' 的值没有成员 'put'

ios - 计算器中的历史记录用照片代替符号

Android 4 条尺寸(Galaxy Nexus)