gpt4 book ai didi

ios - 我们如何在不使用 nib 文件的情况下在 iPhone 应用程序中加载用户界面?

转载 作者:行者123 更新时间:2023-11-28 18:22:33 25 4
gpt4 key购买 nike

我们如何在不使用 nib 文件的情况下在 iPhone 应用程序中加载用户界面?我想使用 .m 文件加载应用程序的用户界面。这可能吗?我们该怎么做。

最佳答案

如果您想以编程方式执行此操作,则 loadView方法用于在不使用 NIB 或 Storyboard时创建 View 。只需在您的 View Controller 中编写一个 loadView 并在那里创建您的 View 。或者让 loadView 创建一个 UIView 子类的实例,并将自定义 View 的大部分代码放入该 UIView 子类中。这是使用 Interface Builder 构建 NIB 或 Storyboard的替代方法。

如果您在 loadView 中执行此操作,只需确保将 self.view 设置为您创建的 View ,然后其余的 View Controller 代码就可以像其他方式一样引用 view 属性。


参见 Creating a View Programmatically遗留 View Controller Programming Guide 部分。 它概述了基本过程并提供了代码示例。诚然,一些遗留文档并不完全相关(例如,值得注意的是, View 不会在内存压力下“卸载”,因此对 viewDidUnload 的任何引用都不再相关),但它提供了一些上下文以编程方式创建 View 层次结构。

A View Controller Instantiates Its View Hierarchy When Its View is Accessed [断开的链接] 该指南的部分用于概述基本流程:

enter image description here


例如,这是一个 Swift 3 示例 loadView,用于以编程方式创建其 View 层次结构的 View Controller :

class ViewController: UIViewController {

weak var containerView: UIView!
weak var button: UIButton!

// Note, when programmatically creating views, you:
//
// - Implement the view hierarchy in `loadView` (not to be confused with `viewDidLoad`)
// - Must set `view` property
// - Must not call `super.loadView`
//
// This example creates a `view` for the view controller, and adds two subviews, a container and a button.
// Note, the "container" isn't really needed, but I just did it so I could visually demonstrate that the
// `frame` of the view was set properly. That's why I set it to be a different color of the main `view`
// and inset it.

override func loadView() {
view = UIView()
view.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)

let containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
self.containerView = containerView

let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
button.setTitle("Back", for: .normal)
button.setTitleColor(.white, for: .normal)
containerView.addSubview(button)
self.button = button

NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 10),
containerView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: -10),
containerView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10),
containerView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10),

button.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
button.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)
])

}

func didTapButton(_ sender: Any) {
// do something
}

}

关于ios - 我们如何在不使用 nib 文件的情况下在 iPhone 应用程序中加载用户界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17417570/

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