gpt4 book ai didi

iOS 应用程序未呈现 View

转载 作者:可可西里 更新时间:2023-11-01 00:43:34 26 4
gpt4 key购买 nike

我想测试一些 Swift 代码,但我发现了一个我以前从未见过的问题。当我在 iPhone 模拟器或真实设备上启动应用程序时,出于某种原因, View 没有显示......我在创建项目后只做了两件事:

  1. 在 AppDelegate 文件中编写一些代码来创建窗口,然后选择初始 Controller 。
  2. 在该 Controller 的 viewDidLoad 方法上创建一个简单的 UITextView

我唯一得到的是一个全白的屏幕,中间没有任何文本,因为它应该是这样的。我用了一个print("Called")来验证viewDidLoad是否正在执行,其实就是这样……问题出在哪里?

AppDelegate.swift:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = window ?? UIWindow()
window?.backgroundColor = UIColor.white

window?.rootViewController = ViewController()

window?.makeKeyAndVisible()

return true
}

}

extension CGRect {

init(_ x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat) {
self.init(x: x, y: y, width: w, height: h)
}

}

ViewController.swift:

class ViewController: UIViewController {

override func viewDidLoad() {

print("Called")

let tv = UITextView()
tv.text = "Hola Mundo"
tv.sizeToFit()
tv.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(tv)

NSLayoutConstraint.activate([
tv.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
tv.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])

}

}

最佳答案

开头问题中的 View 在屏幕上您只是看不到它,因为它的高度和宽度已被自动布局压缩为零。有多种方法可以改变这一点,将 isScrollEnabled 设置为 false 是最简单的 ( see here )。

居中和自动调整 TextView (无滚动)

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let tv = UITextView()
tv.text = "Hola Mundo"

tv.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tv)

// disable scrolling so that view is not allowed to collapse
tv.isScrollEnabled = false

// position view at centre of screen
tv.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
tv.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

}

}

TextView (滚动)与 View 大小相同(不包括顶部状态栏)

否则,如果您想要一个滚动的 TextView ,那么一个选项是以一种防止 TextView 自行折叠的方式锚定 TextView ,例如:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let tv = UITextView()
tv.text = "Hola Mundo"
tv.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tv)

// anchor view to top, bottom, left and right of view
tv.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
tv.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true
tv.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tv.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

}


}

超出框架大小的文本将滚动(按照默认行为)。

居中 TextView ,大小与 View 成比例(滚动)

或者,为 TextView 提供比例大小是一种保持 TextView 居中并同时可滚动的方法。

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let tv = UITextView()
tv.text = "Hola Mundo"

tv.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tv)

// anchor view to centre of screen
tv.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
tv.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

// set height and width relative to superview height and width
tv.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5).isActive = true
tv.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.75).isActive = true

}

}

说明

这些工作的原因是自动布局与 View 一起工作的方式。简而言之,启用滚动的 View 无法抵抗被允许折叠,而没有启用滚动的 View 则无法被压缩。自动布局就是允许根据赋予它们的优先级(和 anchor )扩展和收缩事物。

关于iOS 应用程序未呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42115820/

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