gpt4 book ai didi

ios - UIToolbarItems 和 UIBarButtonItems 没有出现在模拟器中或被切断,我错过了什么吗?

转载 作者:行者123 更新时间:2023-11-29 05:47:47 25 4
gpt4 key购买 nike

我已经使用一些UIBarButtonItems以编程方式创建了一个WKWebView。右上角有一个“打开”按钮,底部有 3 个用于 ProgressView、flexibleSpace 和重新加载按钮的 UIToolbarItems。我的模拟器无法将这些加载到屏幕上,并且看起来在模拟器上加载不正确。

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var progressView: UIProgressView!
var websites = ["apple.com", "google.com"]

//loadView() func
override func loadView() {

//create an instance of WKWebView()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}

override func viewDidLoad() {
super.viewDidLoad()

//get URL and make a load URLRequest
guard let url = URL(string: "https://www." + websites[0]) else { return }
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true

//set BarButtonItem on the top right of page
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped))

//add UIToolbar items with UIBarButtonItems
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let reload = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))

//add progressView
progressView = UIProgressView(progressViewStyle: .default)
progressView.sizeToFit()
let progressButton = UIBarButtonItem(customView: progressView)

//add items to toolbarItems array
toolbarItems = [progressButton, spacer, reload]
navigationController?.isToolbarHidden = false

//create actionSheet UIAlertActionController for bar button drop down
@objc func openTapped() {
let alert = UIAlertController(title: "Open new page!", message: nil, preferredStyle: .actionSheet)

for website in websites {
alert.addAction(UIAlertAction(title: website, style: .default, handler: openPage))
}

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
present(alert, animated: true)
}

//handlers for each UIAlertAction
func openPage(action: UIAlertAction) {
guard let actionTitle = action.title else { return }
guard let url = URL(string: "https://" + actionTitle) else { return }
webView.load(URLRequest(url: url))
}

//set webView title to web page title
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}

我尝试过不同的屏幕尺寸,但似乎没有一个能让 UIBarButtonItems 和 UIToolbarItems 出现在模拟器上。

what my iphoneXR simulator looks like at the moment

最佳答案

您需要将 ViewController 嵌入到导航 Controller 中。

打开 Storyboard -> 编辑器 -> 嵌入 -> 导航 Controller 。

此外,您的代码在 navigationController?.isToolbarHidden = false 之后缺少一个大括号来关闭 viewDidLoad 方法。

关于ios - UIToolbarItems 和 UIBarButtonItems 没有出现在模拟器中或被切断,我错过了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55978838/

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