Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将视图添加为某些控制器的子视图_Ios_Swift_Uiviewcontroller_Uinavigationcontroller_Uiwindow - Fatal编程技术网

Ios 如何将视图添加为某些控制器的子视图

Ios 如何将视图添加为某些控制器的子视图,ios,swift,uiviewcontroller,uinavigationcontroller,uiwindow,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,Uiwindow,我的应用程序中有多个故事板。我想为一些控制器在导航栏的正下方添加一个始终位于顶部的视图。我怎样才能做到这一点 我已经使用了导航代理并在窗口中添加了一个视图,但运气不好。在附加图像中显示灰色视图的步骤如下所示。 1.单击该视图控制器上的按钮;灰色视图应显示并保持在控制器顶部,直到未完成设备的所有扫描,无论用户是否应使用任何ViewController 您只需使用相关的框架创建一个自定义ui视图,并在视图上调用addSubview(),即可将其添加到其中 lazy var customView: U

我的应用程序中有多个故事板。我想为一些控制器在导航栏的正下方添加一个始终位于顶部的视图。我怎样才能做到这一点

我已经使用了导航代理并在窗口中添加了一个视图,但运气不好。在附加图像中显示灰色视图的步骤如下所示。 1.单击该视图控制器上的按钮;灰色视图应显示并保持在控制器顶部,直到未完成设备的所有扫描,无论用户是否应使用任何ViewController


您只需使用相关的
框架创建一个自定义
ui视图
,并在
视图上调用
addSubview()
,即可将其添加到其中

lazy var customView: UIView = {
    let customView = UIView(frame: CGRect.init(x: 0, y: self.view.safeAreaInsets.top, width: UIScreen.main.bounds.width, height: 100))
    customView.backgroundColor = .gray
    return customView
}()

@IBAction func onTapButton(_ sender: UIButton) {
    self.view.addSubview(customView)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.customView.removeFromSuperview()
}
要将其添加到
导航栏下方
,请使用
y
帧的位置作为
self.view.safeAreaInsets.top
。这样,您的
customView
将始终在
导航栏下方对齐

您可以根据需要创建高度为
的视图。我使用了
height=100

给出正确的
框架
,您可以将任何视图作为
子视图添加到另一个视图中


您可以创建UINavigationController子类并在其中添加视图

class NavigationController: UINavigationController {

    let customView = UIView()
    let iconImgView = UIImageView()
    let msgLbl = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        customView.isHidden = true
        customView.translatesAutoresizingMaskIntoConstraints = false
        customView.backgroundColor = .gray
        view.addSubview(customView)

        iconImgView.contentMode = .scaleAspectFit
        iconImgView.translatesAutoresizingMaskIntoConstraints = false
        customView.addSubview(iconImgView)

        msgLbl.numberOfLines = 0
        msgLbl.lineBreakMode = .byWordWrapping
        msgLbl.textColor = .white
        msgLbl.translatesAutoresizingMaskIntoConstraints = false
        customView.addSubview(msgLbl)

        customView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        customView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        customView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

        iconImgView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        iconImgView.heightAnchor.constraint(equalToConstant: 40).isActive = true
        iconImgView.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
        iconImgView.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 15).isActive = true
        iconImgView.trailingAnchor.constraint(equalTo: msgLbl.leadingAnchor, constant: 15).isActive = true

        msgLbl.topAnchor.constraint(equalTo: customView.topAnchor, constant: 10).isActive = true
        msgLbl.bottomAnchor.constraint(equalTo: customView.bottomAnchor, constant: 10).isActive = true
        msgLbl.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -15).isActive = true
        msgLbl.heightAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true
    }

    func showCustomView(message: String, icon: UIImage) {
        msgLbl.text = message
        iconImgView.image = icon
        customView.isHidden = false
    }
    func hideCustomView() {
        customView.isHidden = true
    }
}
在此导航控制器中嵌入所有视图控制器。如果要在视图控制器中显示/隐藏灰色视图,请使用

展示

(self.navigationController as? NavigationController)?.showCustomView(message: "Any Message", icon: UIImage(named: "anyImage")!)
隐藏

(self.navigationController as? NavigationController)?.hideCustomView()

当您从同一导航控制器推送另一个视图控制器时,视图将不会隐藏,直到您调用隐藏方法

是否希望该视图始终位于导航控制器的导航栏下?@Rico Crescenzio-是的,我希望该视图始终位于导航控制器的导航栏下,但对于某些控制器也是如此是否尝试将视图添加到navigationController视图?类似于
navigationController?.view.addSubview(grayView)
它将在导航视图上添加视图,而不是在导航视图下方。是的,您必须调整视图位置,例如,向导航添加约束bar@PGDev-这将为所有控制器添加视图,但我想为一些控制器添加此视图,然后对特定控制器使用
self.view.addSubview(customView)
。此外,如果没有帮助,请进一步澄清要求。