You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除main.storyboard中Wrapper、back和menu按钮并保留加载条且不报错

操作步骤

第一步:调整Main.storyboard配置

  • 打开Main.storyboard,在左侧场景列表中找到back返回按钮、menu菜单按钮对应的UIBarButtonItem元素,先右键点击两个按钮,在弹出的关联面板中移除所有Outlet和Action连线,再删除两个按钮本身。
  • 找到名为"Wrapper"的元素,确认Wrapper内部包含的webViewContainer、offlineView、activityIndicatorView三个视图(这三个是进度条、离线提示功能必需的元素,不能删除),将这三个子视图从Wrapper中拖到Wrapper原有的父视图层级下,重新给三个视图添加和父视图匹配的约束,确认约束无冲突后删除Wrapper元素。

第二步:修改ViewController.swift代码

  1. 移除无用的Outlet声明,删除// MARK: Outlets下的两行代码:
@IBOutlet weak var leftButton: UIBarButtonItem!
@IBOutlet weak var rightButton: UIBarButtonItem!
  1. 删除两个按钮对应的点击事件方法,整段删除onLeftButtonClick、onRightButtonClick两个函数。
  2. 修改观察者回调逻辑,找到observeValue方法,删除里面的rightButton.isEnabled = (webView.estimatedProgress == 1)这一行,修改后代码如下:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == #keyPath(WKWebView.isLoading)) {
        // 原注释保留即可
    }
    if (keyPath == #keyPath(WKWebView.estimatedProgress)) {
        progressBar.progress = Float(webView.estimatedProgress)
    }
}
  1. 修改UI初始化逻辑,找到setupUI方法,删除所有和左右按钮、横竖屏切换监听相关的代码,删除后的setupUI保留以下内容即可:
func setupUI() {
    // 进度条初始化保留
    progressBar = UIProgressView(frame: CGRect(x: 0, y: 0, width: webViewContainer.frame.width, height: 40))
    progressBar.autoresizingMask = [.flexibleWidth]
    progressBar.progress = 0.0
    progressBar.tintColor = progressBarColor
    webView.addSubview(progressBar)
    
    // 加载指示器配置保留
    activityIndicator.color = activityIndicatorColor
    activityIndicator.startAnimating()
    
    // 离线页配置保留
    offlineIcon.tintColor = offlineIconColor
    offlineButton.tintColor = buttonColor
    offlineView.isHidden = true
    
    // 导航栏配置保留
    if (forceLargeTitle) {
        if #available(iOS 11.0, *) {
            navigationItem.largeTitleDisplayMode = UINavigationItem.LargeTitleDisplayMode.always
        }
    }
    if (useLightStatusBarStyle) {
        self.navigationController?.navigationBar.barStyle = UIBarStyle.black
    }
}
  1. 删除无用工具方法,整段删除isWideScreen、updateRightButtonTitle两个函数。
  2. 修改内存清理逻辑,找到deinit方法,删除横竖屏监听的移除代码,修改后如下:
deinit {
    webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.isLoading))
    webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress))
}

第三步:编译验证

按Command+Shift+K清理项目缓存后重新编译运行,即可保留加载进度条的全部功能,同时不存在多余元素。


内容的提问来源于stack exchange,提问作者Secux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 13:57:04