以下代码可能会使正在运行的应用程序在 iPhone(不是 iPad)或 iPhone 模拟器上无响应。Xcode 显示应用程序消耗 100% CPU,同时分配越来越多的内存。
struct SecondView: View {
@State private var keyboardHeight: CGFloat = 0
private let showPublisher = NotificationCenter.Publisher.init(
center: .default,
name: UIResponder.keyboardWillShowNotification
).map { (notification) -> CGFloat in
if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect {
return rect.size.height
} else {
return 0
}
}
var body: some View {
VStack(spacing: 20) {
if keyboardHeight == 0 {
Text("This is shown as long as there's no keyboard")
}
Text("This is the SecondView. Drag from …
Run Code Online (Sandbox Code Playgroud) 我有以下代码,这使得可以在我的 SwiftUI 代码中使用 UIKit UIScrollView
。可以将其粘贴到新的 SwiftUI 项目中。
struct LegacyScrollView<Content: View>: UIViewRepresentable {
enum Action {
case idle
case offset(x: CGFloat, y: CGFloat, animated: Bool)
}
@Binding var action: Action
let content: Content
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UIScrollView {
let hosting = UIHostingController(rootView: self.content)
hosting.view.translatesAutoresizingMaskIntoConstraints = false
let uiScrollView = UIScrollView()
uiScrollView.addSubview(hosting.view)
let constraints = [
hosting.view.leadingAnchor.constraint(equalTo: uiScrollView.leadingAnchor),
hosting.view.trailingAnchor.constraint(equalTo: uiScrollView.trailingAnchor),
hosting.view.topAnchor.constraint(equalTo: uiScrollView.contentLayoutGuide.topAnchor),
hosting.view.bottomAnchor.constraint(equalTo: uiScrollView.contentLayoutGuide.bottomAnchor),
hosting.view.widthAnchor.constraint(equalTo: uiScrollView.widthAnchor)
]
uiScrollView.addConstraints(constraints)
return uiScrollView
}
func updateUIView(_ …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
struct Credential: Equatable {
var username = ""
var password = ""
}
enum Database {
static var credential = Credential()
}
struct UsernamePasswordInput: View {
@Binding var credential: Credential
var body: some View {
Group {
TextField("Username", text: self.$credential.username)
SecureField("password", text: self.$credential.password)
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
struct Login: View {
private var credential: Binding<Credential>
var body: some View {
UsernamePasswordInput(credential: self.credential)
}
init() {
self.credential = Binding<Credential>(
get: {
Database.credential
}, set: { newValue in
Database.credential = newValue …
Run Code Online (Sandbox Code Playgroud) 这是我的.gitignore文件的内容,它位于我的存储库的根目录:
# grails stuff to ignore
target
*.iml
*.ipr
.idea
*.log
*.swp # vi swap files
.DS_Store # OS X Finder cache files
# cocoapods stuff to ignore
Pods
Podfile.lock
Run Code Online (Sandbox Code Playgroud)
.DS_Store
我的项目根目录中的文件被正确忽略,但git status给出:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
ios/.DS_Store
nothing added to commit but untracked files present (use "git …
Run Code Online (Sandbox Code Playgroud) 我有一个UITableView与节标题.对于单元格和标题,整个tableview都设置了UITableViewAutomaticDimension:
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet var sectionHeader: MyTableViewHeaderFooterView!
let data = [
"Lorem ipsum dolor sit amet",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
]
override func viewDidLoad() { …
Run Code Online (Sandbox Code Playgroud)