将 UIView 框架设置为等于来自不同超级视图的另一个视图的框架
Set UIView frame equal to the frame of another view from a different superview
我正在创建位于不同父视图中的 UIView,但我想将框架设置为彼此相等。
这是我目前尝试过的方法
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
但我的视图最终看起来像这样
使用您提供的对象名称,这是一个创建第二个视图的示例,也是一个 UIButton (subViewOfInnerUIView),它将与第一个按钮 (drawButton) 具有相同的框架。这两个按钮都是主视图中不同父视图的子视图。我使用了一个 UIButton,所以我可以标记视图,但是可以设置框架的 UIView 的任何其他子类也可以工作。
请注意,由于它们具有相同的框架,除了具有相同的大小外,它们相对于父视图的位置也相同。
即使有问题的对象驻留在深或浅几层的子视图中,这也应该有效。应该没关系。
示例可以在最新的 XCode 的单视图 playground 中重新创建。希望这对您有所帮助!
//: 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 的最后一行基本上就是您想要的。您可能可以创建一个函数来完成这 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)
希望对您有所帮助!
我正在创建位于不同父视图中的 UIView,但我想将框架设置为彼此相等。
这是我目前尝试过的方法
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
但我的视图最终看起来像这样
使用您提供的对象名称,这是一个创建第二个视图的示例,也是一个 UIButton (subViewOfInnerUIView),它将与第一个按钮 (drawButton) 具有相同的框架。这两个按钮都是主视图中不同父视图的子视图。我使用了一个 UIButton,所以我可以标记视图,但是可以设置框架的 UIView 的任何其他子类也可以工作。
请注意,由于它们具有相同的框架,除了具有相同的大小外,它们相对于父视图的位置也相同。
即使有问题的对象驻留在深或浅几层的子视图中,这也应该有效。应该没关系。
示例可以在最新的 XCode 的单视图 playground 中重新创建。希望这对您有所帮助!
//: 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 的最后一行基本上就是您想要的。您可能可以创建一个函数来完成这 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)
希望对您有所帮助!