首页 > 解决方案 > XIB 文件在 iOS Swift 中适合全屏

问题描述

我一直在尝试UIViewController在使用当前模式时在整个屏幕上显示?

这是输出图像

[![在此处输入图像描述][1]][1]

我有 xib viewController,我试图使用 present 方法将值发送到另一个 viewController,但它不适合全屏显示。

这是代码:

let vc = TabBarViewController()
vc.processDefID = keyVal

vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vc.modalPresentationStyle = .overFullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)

另一种方法尝试:

 var clientView: UIViewcontroller

 let V = TabBarViewController()
            self.clientView?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.clientView?.addChild(V)
            self.clientView?.view.addSubview(V.view)
            V.processDefID = keyVal
        V.view.frame = (self.clientView?.view.bounds)!
            V.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            V.modalPresentationStyle = .fullScreen
            self.present(V, animated: true, completion: nil)

标签: iosswiftxcodeuinavigationcontrollerpresentmodalviewcontroller

解决方案


通常,您可以通过两种方式显示/呈现您的视图。

创建UIViewController对象并以您正在做的方式呈现它

func presentView() {
  let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ScannerViewController")        
  vc.view.backgroundColor = .red
  vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  vc.modalPresentationStyle = .fullScreen
  self.present(vc, animated: true, completion: nil)
}

UIViewControllerXIB文件一起使用的情况下,它只会更改 ViewController 初始化部分而不是其他任何东西,就像

let vc = ScannerViewController(nibName: "ScannerView", bundle: nil)

创建UIViewController对象并按以下方式插入它,您还可以将自定义框架分配给它的UIView对象。

func presentView() {
                let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ScannerViewController")
                vc.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height/2)
                vc.view.backgroundColor = .red
                self.addChild(vc)
                self.view.addSubview(vc.view)
              }

推荐阅读