gpt4 book ai didi

ios - 如何在 iOS Swift 中向多个 ViewController 添加一个公共(public) View ?

转载 作者:搜寻专家 更新时间:2023-11-01 05:48:43 27 4
gpt4 key购买 nike

我对 iOS 开发有点陌生。
我想要一个自定义 View 和其中的一些标签。在我的应用程序的某些 viewControllers 中单击按钮我想添加/显示该 View 在那个 viewController 的底部。

就我要在其中显示 View 的所有 viewController 中手动添加 Storyboard 中的 View 而言。但这效率不高。如何在单击按钮时以编程方式在 viewControllers 中添加此 View ?

最佳答案

用UIViewController继承一个BaseViewController类

现在在 BaseViewController 中创建名为 designFooter 的方法

func designFooter() {
var viewFooter: UIView = UIView(frame: CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50))
viewFooter.backgroundColor = UIColor.redColor()
self.view!.addSubview(viewFooter)
}

对于 Swift 4、5.1:

func designFooter() {
let viewFooter: UIView = UIView(frame: CGRect(x:0, y:self.view.bounds.size.height - 50, width:self.view.bounds.size.width, height:50))
viewFooter.backgroundColor = UIColor.red
self.view.addSubview(viewFooter)
}

现在将此 BaseViewController 继承给您要在其中添加页脚的 ViewController,并在单击按钮时调用 self.designFooter()

关于ios - 如何在 iOS Swift 中向多个 ViewController 添加一个公共(public) View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com