I'm working on Swift Playground right now. I declared UI objects in loadView()
Swift
class ViewController: UIViewController {
override func loadView() {
let view = UIView(frame: viewRect)
view.backgroundColor = UIColor.white
let titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.text = "title"
titleLabel.backgroundColor = .gray
view.addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.widthAnchor.constraint(equalToConstant: 200),
titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
let subLabel = UILabel()
subLabel.translatesAutoresizingMaskIntoConstraints = false
subLabel.text = "title"
subLabel.backgroundColor = .red
view.addSubview(subLabel)
NSLayoutConstraint.activate([
subLabel.widthAnchor.constraint(equalToConstant: 100),
subLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 100),
subLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
self.view = view
}
}
in this case, Everything works fine.
But I tried to declare those labels outside of loadView() like this:
Swift
class ViewController: UIViewController {
var titleLabel = UILabel()
var subLabel = UILabel()
override func loadView() {
let view = UIView(frame: viewRect)
view.backgroundColor = UIColor.white
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.text = "title"
titleLabel.backgroundColor = .gray
view.addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.widthAnchor.constraint(equalToConstant: 200),
titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
subLabel.translatesAutoresizingMaskIntoConstraints = false
subLabel.text = "title"
subLabel.backgroundColor = .red
view.addSubview(subLabel)
NSLayoutConstraint.activate([
subLabel.widthAnchor.constraint(equalToConstant: 100),
subLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 100),
subLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
self.view = view
}
}
When I run this code (which labels are declared outside of loadView()), Constraints are not working except for width/height constraints.
Also, when i run the code only with 1 label, Everything works fine even label is declared outside of loadView()
Is there any solution to fix this problem?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Greetings, I want to force my app's Orientation.
Most solutions for "How to force orientation to Landscape for SwiftUI" seems works only for "real" SwiftUI project, not .swiftpm
How can I force orientation to landscape for Swift Playground App Project?
Greetings, I want to force my app's Orientation. Most solutions for "How to force orientation to Landscape for SwiftUI" seems works only for "real" SwiftUI project, not .swiftpm
How can I force orientation to landscape for Swift Playground App Project?
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
Swift Playground
Swift Student Challenge
SwiftUI
Playground Support
Hi. Now I'm on submitting process.
For submitting .zip section, there's
Which software should we use to run your app project?
with two options which is:
Swift Playgrounds 4.0.2 on iPadOS 15.3
Xcode 13.2.1 on macOS 12.2.1
I developed my app on Xcode but target of my app is iPad
Should I select Swift Playgrounds 4.0.2 on iPadOS 15.3 ?
(I cannot run my app on my Mac because I'm using intel-Mac)