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

标签 ios swift uiview uibarbuttonitem uitoolbaritem

我已经使用一些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/

相关文章:

iOS - UI 测试检查视频是否播放

iphone - 如何在 iOS 中动态创建表单

ios - 在哪里可以找到有关 iOS 应用程序中安全网络的教程?

ios - 在touchesBegan 和touchesEnded 之间是否有调用的方法?

ios - 将手势转发到 super View

ios - 如何处理 iOS 上的容器目录?

swift - 完成后更改滑动按钮标题

json - 如何使用 JSON 中的嵌套类型在 Swift 中表示字典

objective-c - 将 NSLocalizedRecoveryOptionsErrorKey 设置为 NSError UserInfo 不提供任何恢复选项

ios - CGAffineTransformRotate 之后 CGRectIntersection 不起作用