将UIView框架设置为与另一个超级视图中另一个视图的框架相等

时间:2019-11-24 21:45:34

标签: ios swift frame

我正在创建处于不同超级视图中的UIView,但是我想将框架设置为彼此相等。

enter image description here

这是我到目前为止尝试过的

let globalPoint = self.view.convert(subViewOfOuterView.frame.origin, to: nil)
let frame = CGRect(x: globalPoint.x, y: globalPoint.y, width: self.subViewOfOuterView.frame.width, height: self.subViewOfOuterView.frame.height)
subViewOfInnerUIView.frame = frame

但是我的观点最终看起来像这样

enter image description here


更新:Wattholms解决方案的输出

enter image description here

1 个答案:

答案 0 :(得分:1)

使用您提供的对象名称,这是一个示例,该示例创建第二个视图,也就是UIButton(subViewOfInnerUIView),其框架与第一个按钮(drawButton)相同。这两个按钮都是主视图中不同超级视图的子视图。我使用了UIButton,因此可以标记视图,但是可以设置框架的UIView的任何其他子类也可以使用。

请注意,由于它们具有相同的框架,除了具有相同的尺寸外,它们相对于其父视图的位置也相同。

即使有问题的对象位于几层深或浅的子视图中,这也应该起作用。没关系。

可以在最新的XCode的单视图操场上重新创建示例。希望这会有所帮助!

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        self.view = view

        let subView1 = UIView(frame: CGRect(x: 40, y: 250, width: 300, height: 300))
        subView1.backgroundColor = .red
        view.addSubview(subView1)

        let subView2 = UIView(frame: CGRect(x: 20, y: 50, width: 340, height: 100))
        subView2.backgroundColor = .green
        view.addSubview(subView2)

        let drawButton = UIButton(frame: CGRect(x: subView1.frame.width / 2 - 50, y: 25, width: 150, height: 50))
        drawButton.backgroundColor = .blue
        drawButton.setTitle("DRAW BTN", for: .normal)
        subView1.addSubview(drawButton)

        let subViewOfInnerUIView = UIButton()
        subViewOfInnerUIView.setTitle("DRAW BTN2", for: .normal)
        subViewOfInnerUIView.backgroundColor = .brown
        subView2.addSubview(subViewOfInnerUIView)

        let frame = view.convert(drawButton.frame, to: nil)
        subViewOfInnerUIView.frame = frame
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

这里是更新的代码,用于使两个视图重叠。我对更改进行了动画处理,使其很明显,还评论了特定的框架以尝试解释其完成方式:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        self.view = view

        let subView1 = UIView(frame: CGRect(x: 40, y: 250, width: 300, height: 300))
        subView1.backgroundColor = .red
        view.addSubview(subView1)

        let subView2 = UIView(frame: CGRect(x: 20, y: 50, width: 340, height: 100))
        subView2.backgroundColor = .green
        view.addSubview(subView2)

        let drawButton = UIButton(frame: CGRect(x: subView1.frame.width / 2 - 50, y: 25, width: 150, height: 50))
        drawButton.backgroundColor = .blue
        drawButton.setTitle("DRAW BTN", for: .normal)
        subView1.addSubview(drawButton)

        let subViewOfInnerUIView = UIButton()
        subViewOfInnerUIView.setTitle("DRAW BTN2", for: .normal)
        subViewOfInnerUIView.backgroundColor = .brown
        subView2.addSubview(subViewOfInnerUIView)

        let frame1 = drawButton.frame
        let frame2 = subView1.convert(drawButton.frame, to: view)
        let frame3 = view.convert(frame2, to: subView2)

        print(frame1) // original drawButton frame in its superview
        print(frame2) // drawButton frame relative to the main view (self.view)
        print(frame3) // drawButton frame relative to subView2 (this is the frame you want)

        subViewOfInnerUIView.frame = view.convert(drawButton.frame, to: nil)

        UIView.animate(withDuration: 5.0, delay: 0.0, options: [.autoreverse, .repeat], animations: {
            subViewOfInnerUIView.frame = frame3
        }, completion: nil)

        subViewOfInnerUIView.frame = frame3


    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

将subViewOfInnerUIView.frame设置为frame3的最后一行基本上就是您想要的。您可能可以创建一个函数来完成这三行代码的工作,以使其变得更容易(以2个子视图作为参数),尽管公认的是,如果视图层次很深,它可能并不那么简单:

    let frame2 = subView1.convert(drawButton.frame, to: view)
    let frame3 = view.convert(frame2, to: subView2)
    subViewOfInnerUIView.frame = view.convert(drawButton.frame, to: nil)

希望这会有所帮助!