子视图在 xcode 操场上不工作
Subview not working in xcode playground
我试图将 UIView 类型的子视图添加到我的主视图。但是,这不起作用。我认为这应该将屏幕更改为红色并显示文本。你们中有人知道问题出在哪里吗?
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
func loadView() {
let secondViewController = SecondView()
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
secondViewController.addSubview(label)
print("Test")
}
}
class MyViewController : UIViewController {
//Outlets
let profileButton = UIButton()
override func loadView() {
let view = UIView()
view.backgroundColor = .green
print(view.frame)
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
view.addSubview(sView)
print(sView.frame)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
loadView
是一个 UIViewController
生命周期回调方法(参见 docs)。
您的 SecondView
是 UIView
的子类,未定义 loadView
。因此,您在 SecondView
中实现的 loadView
不会被环境调用。您必须明确地调用它:
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
sView.loadView()
view.addSubview(sView)
此外,我建议重命名它(这样它会与 UIViewController
的 loadView
.
混淆
更新
那里还有其他几个错误。
1:在第二个视图的 loadView
中,您正在创建一个新的 SecondView
并向其添加标签。您需要将其添加到 self
.
2:在 viewController 的 loadView
中,您正在创建一个新的 view
和 UIView()
,默认为UIView(frame: CGRect.zero)
然后你设置 sView.frame = view.frame
这将使第二个视图的框架也成为 CGRect.zero
。 view
将被 UIViewController
环境拉伸,但 sView
的框架不会更新。有几种方法可以做到这一点,但最简单的方法是从一开始就用适当的 frame
初始化 view
。工作游乐场解决方案:
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
func loadView() {
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
self.addSubview(label)
print("Test")
}
}
class MyViewController : UIViewController {
//Outlets
let profileButton = UIButton()
override func loadView() {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .green
print(view.frame)
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
sView.loadView()
view.addSubview(sView)
print(sView.frame)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
声明子视图的正确方法
class SecondView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
sharedLayout()
}
required init(coder aDecoder: NSCoder) {
super.init(coder : aDecoder)
sharedLayout()
}
func sharedLayout()
{
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
self.addSubview(label)
print("Test")
}
}
你做错了很多事情:(
这将 运行 在 playground 页面中,并且应该接近您的目标。查看评论以了解发生了什么:
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
// create a label
let label = UILabel()
// we're going to use auto-layout
label.translatesAutoresizingMaskIntoConstraints = false
// add the label to self
self.addSubview(label)
// pin the label to leading, trailing and bottom -- all to Zero
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0).isActive = true
label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0.0).isActive = true
label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
}
}
class MyViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// we don't need to create a new view
//let view = UIView()
view.backgroundColor = .green
// create the "SecondView"
let sView = SecondView()
// we're going to use auto-layout
sView.translatesAutoresizingMaskIntoConstraints = false
// add the new view
view.addSubview(sView)
// pin the new view to top, leading and trailing -- all to Zero so it fills this view
sView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
sView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
sView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
sView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true
sView.backgroundColor = .red
}
}
// Present the view controller in the Live View window
let vc = MyViewController()
vc.preferredContentSize = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc
结果:
我试图将 UIView 类型的子视图添加到我的主视图。但是,这不起作用。我认为这应该将屏幕更改为红色并显示文本。你们中有人知道问题出在哪里吗?
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
func loadView() {
let secondViewController = SecondView()
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
secondViewController.addSubview(label)
print("Test")
}
}
class MyViewController : UIViewController {
//Outlets
let profileButton = UIButton()
override func loadView() {
let view = UIView()
view.backgroundColor = .green
print(view.frame)
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
view.addSubview(sView)
print(sView.frame)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
loadView
是一个 UIViewController
生命周期回调方法(参见 docs)。
您的 SecondView
是 UIView
的子类,未定义 loadView
。因此,您在 SecondView
中实现的 loadView
不会被环境调用。您必须明确地调用它:
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
sView.loadView()
view.addSubview(sView)
此外,我建议重命名它(这样它会与 UIViewController
的 loadView
.
更新
那里还有其他几个错误。
1:在第二个视图的 loadView
中,您正在创建一个新的 SecondView
并向其添加标签。您需要将其添加到 self
.
2:在 viewController 的 loadView
中,您正在创建一个新的 view
和 UIView()
,默认为UIView(frame: CGRect.zero)
然后你设置 sView.frame = view.frame
这将使第二个视图的框架也成为 CGRect.zero
。 view
将被 UIViewController
环境拉伸,但 sView
的框架不会更新。有几种方法可以做到这一点,但最简单的方法是从一开始就用适当的 frame
初始化 view
。工作游乐场解决方案:
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
func loadView() {
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
self.addSubview(label)
print("Test")
}
}
class MyViewController : UIViewController {
//Outlets
let profileButton = UIButton()
override func loadView() {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .green
print(view.frame)
let sView = SecondView()
sView.backgroundColor = .red
sView.frame = view.frame
sView.loadView()
view.addSubview(sView)
print(sView.frame)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
声明子视图的正确方法
class SecondView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
sharedLayout()
}
required init(coder aDecoder: NSCoder) {
super.init(coder : aDecoder)
sharedLayout()
}
func sharedLayout()
{
let label = UILabel()
label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
self.addSubview(label)
print("Test")
}
}
你做错了很多事情:(
这将 运行 在 playground 页面中,并且应该接近您的目标。查看评论以了解发生了什么:
import UIKit
import Foundation
import PlaygroundSupport
class SecondView : UIView{
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
// create a label
let label = UILabel()
// we're going to use auto-layout
label.translatesAutoresizingMaskIntoConstraints = false
// add the label to self
self.addSubview(label)
// pin the label to leading, trailing and bottom -- all to Zero
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0).isActive = true
label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0.0).isActive = true
label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true
label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white
}
}
class MyViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// we don't need to create a new view
//let view = UIView()
view.backgroundColor = .green
// create the "SecondView"
let sView = SecondView()
// we're going to use auto-layout
sView.translatesAutoresizingMaskIntoConstraints = false
// add the new view
view.addSubview(sView)
// pin the new view to top, leading and trailing -- all to Zero so it fills this view
sView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
sView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
sView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
sView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true
sView.backgroundColor = .red
}
}
// Present the view controller in the Live View window
let vc = MyViewController()
vc.preferredContentSize = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc
结果: