gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:35:33 25 4
gpt4 key购买 nike

抱歉,如果我的问题相当愚蠢:我目前正在处理屏幕元素,需要根据父 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/

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