代码之家  ›  专栏  ›  技术社区  ›  Sam

将“帧”设置为其他超视图中另一个视图的帧

  •  0
  • Sam  · 技术社区  · 5 年前

    我正在创建不同超视图中的UIViews,但我希望将帧设置为彼此相等。

    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

    0 回复  |  直到 5 年前
        1
  •  1
  •   Wattholm    5 年前

    请注意,由于它们具有相同的帧,除了具有相同的大小之外,它们相对于它们的超视图也具有相同的位置。

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

    可以在最新的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()
    

    最后一行子视图innerview.frame到frame3基本上就是你想要的。您可能可以创建一个函数来完成这3行的工作,以使其更简单(以2个子视图为参数),尽管无可否认,如果视图层次结构很深,它可能不会那么简单:

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