iOS编程:如何将一个UIViewController设为另一个的背景视图?
问题
需要将一个UIImageView作为背景参考图置于另一个UIImageView后方。现有三个ViewController:MainViewController作为主控制器,secondViewController处理核心逻辑,thirdViewController承载背景图。已将secondViewController的mainImageView设为透明,但thirdViewController的背景仍无法显示,求可行的解决思路。
现有代码
MainViewController
import UIKit class MainViewController: UIViewController { let canvasView = secondViewController() let canvasBackground = thirdViewController() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBlue canvas() } func canvas() { addChild(canvasView) view.addSubview(canvasView.view) canvasView.didMove(toParent: self) } }
secondViewController
class secondViewController: UIViewController { private let mainImageView: UIImageView = { var imageView = UIImageView() imageView.backgroundColor = .clear return imageView }() // some more code and logic implementation override func viewDidLoad() { super.viewDidLoad() } }
thirdViewController
class thirdViewController: UIViewController { private let viewLayer: UIImageView = { let layerView = UIImageView() layerView.backgroundColor = .systemGreen return layerView }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(viewLayer) viewLayer.frame = view.bounds } }
解决思路
添加并正确层级布局背景控制器视图
你创建了canvasBackground实例,但未将其视图添加到MainViewController的视图层级,也未做布局。需要先添加背景控制器视图,再添加前景的canvasView视图,确保背景处于下层:func canvas() { // 先处理背景控制器 addChild(canvasBackground) view.addSubview(canvasBackground.view) canvasBackground.didMove(toParent: self) canvasBackground.view.frame = view.bounds // 再处理前景控制器 addChild(canvasView) view.addSubview(canvasView.view) canvasView.didMove(toParent: self) canvasView.view.frame = view.bounds }设置secondViewController的根视图透明
仅将mainImageView设为透明不够,secondViewController的默认根视图背景为不透明色,会完全遮挡下方的背景控制器。必须将根视图背景也设为透明:class secondViewController: UIViewController { // ... 现有代码 override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .clear // 新增该行 } }用Auto Layout替代固定frame(推荐)
固定frame在屏幕旋转或不同尺寸设备上会出现适配问题,建议使用Auto Layout约束来设置视图位置:func canvas() { // 处理背景控制器 addChild(canvasBackground) view.addSubview(canvasBackground.view) canvasBackground.didMove(toParent: self) canvasBackground.view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ canvasBackground.view.topAnchor.constraint(equalTo: view.topAnchor), canvasBackground.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), canvasBackground.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), canvasBackground.view.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) // 处理前景控制器 addChild(canvasView) view.addSubview(canvasView.view) canvasView.didMove(toParent: self) canvasView.view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ canvasView.view.topAnchor.constraint(equalTo: view.topAnchor), canvasView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), canvasView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), canvasView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) canvasView.view.backgroundColor = .clear }
内容的提问来源于stack exchange,提问作者willd
相关产品推荐
相关产品推荐

