如何在Swift中获取UIView中所有子视图的高度?

如何在Swift中获取UIView中所有子视图的高度?,swift,uiview,Swift,Uiview,如何获取UIView中所有子视图的高度并将其存储在公共变量中 例如: var ypos = 20 // initial y position for first UI Controller ypos = ypos + uiheight // Dynamic incrementing of y position of other UIView with respect to the height of the previous UIView. let DynamicView=UIView(

如何获取UIView中所有子视图的高度并将其存储在公共变量中

例如:

var ypos = 20  // initial y position for first UI Controller

ypos = ypos + uiheight  // Dynamic incrementing of y position of other UIView with respect to the height of the previous UIView.

let DynamicView=UIView(frame: CGRectMake(100, ypos, 100, uiheight))
DynamicViews.backgroundColor=UIColor.greenColor()
DynamicViews.layer.cornerRadius=25
DynamicViews.layer.borderWidth=2
self.view.addSubview(DynamicView)

let DynamicViews=UIView(frame: CGRectMake(100, ypos, 100, uiheight))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
self.view.addSubview(DynamicViews)

let DynamicView2=UIView(frame: CGRectMake(100, ypos, 100, uiheight))
DynamicView2.backgroundColor=UIColor.greenColor()
DynamicView2.layer.cornerRadius=25
DynamicView2.layer.borderWidth=2
self.view.addSubview(DynamicView2)

获取当前视图的所有子视图,并使用
map
reduce
获取高度之和

let yPos = (view.subviews.map { $0.frame.height }).reduce(20, +)

获取当前视图的所有子视图,并使用
map
reduce
获取高度之和

let yPos = (view.subviews.map { $0.frame.height }).reduce(20, +)

只需在视图中的子视图中运行一个循环:

var height:CGFloat = 0
for view in self.view.subviews {
    height = height + view.bounds.size.height
}
print(height) //total height of all subviews 
更新:

要为下一个子视图动态设置YPO,请运行一个循环,以查看所需的任意多个视图:

    var yPos: CGFloat = 20 //initial yPos
    let uiHeight: CGFloat = 100 //fixed height for all views

    let someView = UIView(frame: CGRect(x: 100, y: yPos, width: 100, height: uiHeight))
    someView.backgroundColor = UIColor.green
    self.view.addSubview(someView)

    for _ in 0 ..< 3 {
        yPos = uiHeight + yPos + 2 // every time the loop runs, it will add the yPos of the previous view to current yPos
        let dynamicView = UIView(frame: CGRect(x: 100, y: yPos, width: 100, height: uiHeight))
        dynamicView.backgroundColor = UIColor.red
        self.view.addSubview(dynamicView)
    }
给我以下输出:


只需在视图中的子视图中运行一个循环:

var height:CGFloat = 0
for view in self.view.subviews {
    height = height + view.bounds.size.height
}
print(height) //total height of all subviews 
更新:

要为下一个子视图动态设置YPO,请运行一个循环,以查看所需的任意多个视图:

    var yPos: CGFloat = 20 //initial yPos
    let uiHeight: CGFloat = 100 //fixed height for all views

    let someView = UIView(frame: CGRect(x: 100, y: yPos, width: 100, height: uiHeight))
    someView.backgroundColor = UIColor.green
    self.view.addSubview(someView)

    for _ in 0 ..< 3 {
        yPos = uiHeight + yPos + 2 // every time the loop runs, it will add the yPos of the previous view to current yPos
        let dynamicView = UIView(frame: CGRect(x: 100, y: yPos, width: 100, height: uiHeight))
        dynamicView.backgroundColor = UIColor.red
        self.view.addSubview(dynamicView)
    }
给我以下输出:



是的。对不起,我没有看问题中的代码。我使用的是swift 4。你能在4kman上分享它的源代码吗?但我得到的是普通的yPos。我需要添加同一子视图的每个子视图高度+y位置,以获得下一个子视图的新y位置let yPos=(view.subviews.map{$0.bounds.height})。reduce(20,+)print(yPos)let btn=UIButton(frame:CGRect(x:50,y:yPos,width:140,height:20))btn.setTitle(“单击我”,表示:.normal)btn.tintColor=UIColor.black self.view.addSubview(btn)让label1=UILabel(框架:CGRect(x:50,y:yPos,宽度:100,高度:20))label1.text=“Hi Label 1”label1.text对齐=.center label1.textColor=.white label1.backgroundColor=UIColor.blue self.view.addSubview(label1)我不清楚你到底想达到什么目标。如果要访问视图框架的垂直端,应使用
view.frame.maxY
.Yes。对不起,我没有看问题中的代码。我使用的是swift 4。你能在4kman上分享它的源代码吗?但我得到的是普通的yPos。我需要添加同一子视图的每个子视图高度+y位置,以获得下一个子视图的新y位置let yPos=(view.subviews.map{$0.bounds.height})。reduce(20,+)print(yPos)let btn=UIButton(frame:CGRect(x:50,y:yPos,width:140,height:20))btn.setTitle(“单击我”,表示:.normal)btn.tintColor=UIColor.black self.view.addSubview(btn)让label1=UILabel(框架:CGRect(x:50,y:yPos,宽度:100,高度:20))label1.text=“Hi Label 1”label1.text对齐=.center label1.textColor=.white label1.backgroundColor=UIColor.blue self.view.addSubview(label1)我不清楚你到底想达到什么目标。如果要访问视图框架的垂直端,应使用
view.frame.maxY
。我假设初始值
ypos
20
表示状态栏的高度。请注意,状态栏既可以更改高度,也可以隐藏(不占用任何高度)。在这种情况下,最好查看
statusBarFrame
。我假设初始值
ypos
20
表示状态栏的高度。请注意,状态栏既可以更改高度,也可以隐藏(不占用任何高度)。在这种情况下,最好查看
statusBarFrame
。我想更改每个子视图的y位置,计算上一个子视图的高度。请分享来源,因为我是一个新手。提问时请更详细。您的问题是“如何获取UIView中所有子视图的高度并将其存储在公共变量中?”以上是答案。你最好提出一个新问题,详细询问你到底想要什么。不要只要求代码,分享你已经尝试过的代码。所以这不仅仅是为了得到解决方案,而是为了讨论解决方案。例如,若第一个子视图位于y位置=20,子视图的高度为100。然后我需要用一个变量动态地将下一个子视图的y位置设置为120,依此类推。我需要为每个下一个子Viewlet uiHeight:CGFloat=100//动态地创建yPos,以固定所有视图的高度。我们可以保持每个视图的高度不同吗?让YPO保持动态。例如:UILabel height=30,UIImageView height=100,UIButton height=40,然后更改yPos动态必须在创建这些UILabel或UIImages时根据需要进行设置。此时将创建相同类型的动态视图,请一次性详细说明您的问题。停止反复更改问题。我想更改每个子视图的y位置,计算上一个子视图的高度。请分享来源,因为我是一个新手。提问时请更详细。您的问题是“如何获取UIView中所有子视图的高度并将其存储在公共变量中?”以上是答案。你最好提出一个新问题,详细询问你到底想要什么。不要只要求代码,分享你已经尝试过的代码。所以这不仅仅是为了得到解决方案,而是为了讨论解决方案。例如,若第一个子视图位于y位置=20,子视图的高度为100。然后我需要用一个变量动态地将下一个子视图的y位置设置为120,依此类推。我需要为每个下一个子Viewlet uiHeight:CGFloat=100//动态地创建yPos,以固定所有视图的高度。我们可以保持每个视图的高度不同吗?让YPO保持动态。例如:UILabel height=30,UIImageView height=100,UIButton height=40,然后更改yPos动态必须在创建这些UILabel或UIImages时根据需要进行设置。此时将创建相同类型的动态视图,请一次性详细说明您的问题。不要一次又一次地改变你的问题。