【问题标题】:Popup view controller shows dark background when user navigates to another view controller without closing the current pop up view当用户导航到另一个视图控制器而不关闭当前弹出视图时,弹出视图控制器显示深色背景
【发布时间】:2018-08-19 13:24:06
【问题描述】:

我有一个位于标签栏内的表格视图。当我单击表格视图时,它会显示一个弹出视图控制器,如下所示:

但是,每当我导航到另一个标签栏时,弹出视图保持原样而不关闭它,当我回到上一个弹出视图时,它会显示一个深色背景,如下图所示:

我该如何解决这个问题?

*注意 = 我是 IOS 新手

添加子视图的代码:

import UIKit

class NotificationViewController:   UIViewController,UITableViewDelegate,UITableViewDataSource {

var dataALL = [NotificationDataModel]()

@IBOutlet weak var tableViewAll: UITableView!

@IBOutlet  var mainSegmentedControl: UISegmentedControl!

lazy var iNotificationViewController : INotificaitonViewController = {

    let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
    var viewController = storyBoard.instantiateViewController(withIdentifier: "INotificaitonViewController") as! INotificaitonViewController

    self.addViewContollerAsChildViewcontroller(childViewContoller: viewController)
    return viewController

}()

var activityIndicator : UIActivityIndicatorView = UIActivityIndicatorView()


override func viewDidLoad() {
    super.viewDidLoad()
    tableViewAll.delegate = self
    tableViewAll.dataSource = self
    let nib  = UINib(nibName: "TableViewCell", bundle: nil)
    tableViewAll.register(nib, forCellReuseIdentifier: "customCell")
    setupView()
    tableViewAll.separatorStyle = .none
    self.startShowingIndication()
    downloadAllJSONData {
        self.tableViewAll.reloadData()
        self.stopShowingIndication()
        print("sucess")

    }
}

func downloadAllJSONData(completed : @escaping ()->()){
    guard let url = URL(string: "http://www.swipecrafts.com/notice/get")else {return}
    var request = URLRequest.init(url: url)
    request.httpMethod = "POST"
    request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb86c6108391e036387f3d5356a582171519367747000", forHTTPHeaderField: "api_ky")
    let postdata = "school_id=\(SCHOOL_ID)"
    request.httpBody = postdata.data(using: .utf8)

    URLSession.shared.dataTask(with: request) { (data, response, error) in
        if error == nil{
            do{
                self.dataALL = try JSONDecoder().decode([NotificationDataModel].self, from: data!)
                DispatchQueue.main.async {
                    completed()
                    print(self.dataALL.count)
                }
            }catch{
                print(error)
            }
        }
    }.resume()
}

private func setupView(){
    setupSegmentedControl()
    updateView()

}
private func updateView(){
    iNotificationViewController.view.isHidden = !(mainSegmentedControl.selectedSegmentIndex == 0)



}
private func setupSegmentedControl(){
    mainSegmentedControl.removeAllSegments()
    mainSegmentedControl.insertSegment(withTitle: "General Notification", at: 0, animated: false)
    mainSegmentedControl.insertSegment(withTitle: "All Notificatoin", at: 1, animated: false)
    mainSegmentedControl.addTarget(self, action: #selector(selectionDidChange(sender:)), for: .valueChanged)

    mainSegmentedControl.selectedSegmentIndex = 0

}
@objc func selectionDidChange(sender: UISegmentedControl)  {
    updateView()
}

private func addViewContollerAsChildViewcontroller (childViewContoller : UIViewController){
    addChildViewController(childViewContoller)
    view.addSubview(childViewContoller.view)

    childViewContoller.view.frame = view.bounds
    childViewContoller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    childViewContoller.didMove(toParentViewController: self)


}
private func removeViewControllerAsChildViewController(childViewController : UIViewController){
    childViewController.willMove(toParentViewController: nil)
    childViewController.view.removeFromSuperview()
    childViewController.removeFromParentViewController()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataALL.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewAll.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
    cell.customInit(noticeTitle: dataALL[indexPath.row].notice_title!,noticeDiscripetion: dataALL[indexPath.row].notice_description!)
    let downloadedimage = dataALL[indexPath.row].image_name
    if downloadedimage != nil{
        cell.lblImageNotificaiton.downloadedFrom(link: downloadedimage!)
    }
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToPopUpViewww", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? PopUpViewController{
        destination.notificationfianlData = dataALL[(tableViewAll.indexPathForSelectedRow?.row)!]
    }

}
func startShowingIndication(){

    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    view.addSubview(activityIndicator)

    activityIndicator.startAnimating()


}


func stopShowingIndication(){
    activityIndicator.stopAnimating()


}


}

【问题讨论】:

  • 您的弹出窗口是常规 UIViewController 子类吗?
  • 是的,它是@LGP。
  • 你能分享一些代码吗?
  • @JenJose 我已经添加了我的代码,请您检查一下!
  • 我有一个想法可以解决这个问题,但首先要说一下如何打开弹出窗口? [1] UIView 或 [2] UIViewcontroller

标签: ios uitableview uiviewcontroller popup uitabbarcontroller


【解决方案1】:

目前您正在通过 NotificationViewController 的上下文呈现 popupviewcontroller。使用执行 Segue“goToPopUpViewww”。

请尝试在 UITabBarController 的上下文中呈现 popupviewcontroller。这更有意义。这种变化情况是 - 当弹出窗口时,用户无法切换选项卡。

【讨论】:

  • 在这种情况下我该怎么做?
【解决方案2】:

因此,您正在以模态方式展示 Seague,您可以尝试以编程方式或通过故事板更改 seague 的展示风格(例如,在当前上下文中),这在这种情况下可能会有所帮助。 请看截图

【讨论】:

  • 在此示例中,当前上下文是要更改的主要内容。
【解决方案3】:

这是一个来自 Apple 文档的示例项目,已转换为 Swift 3。它使用自定义 UIPresentationController 以模态方式呈现 ViewController,并控制它们在屏幕上和屏幕外的动画方式。遵循相同的方法,深色背景应该不会有任何问题。

https://github.com/ooper-shlab/CustomTransitions-Swift

【讨论】:

    【解决方案4】:

    我会调查UIPopoverPresentationController。

    通常,当您以您所描述的方式呈现某些内容时,用户不应该能够在不首先关闭“弹出”或“弹出”视图的情况下离开父视图控制器。因此,即使您不使用 UIPopoverPresentationController,您也应该考虑通过其外部的任何点击来关闭弹出视图。

    2018 年 4 月 3 日更新

    我肯定会推荐研究和阅读一些文档或遵循一些教程,这样您就可以准确地了解什么代码做了什么以及为什么。如果您还没有我在上面提供的 UIPopoverPresentationController 的文档链接,请指向 this,这正是您所需要的。

    这是您的一些代码的示例。

    lazy var iNotificationViewController : INotificaitonViewController = {
        let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let viewController = storyBoard.instantiateViewController(withIdentifier: "INotificaitonViewController")
        // may want to set the viewController.preferredContentSize here => which will be the size of your popover
        viewController.modalPresentationStyle = .popover
    
        if let popoverPresentationController = viewController.popoverPresentationController {
            popoverPresentationController.delegate = self
            // set the .sourceView and .sourceRect so that the popover can position itself and arrow accordingly
        }
    
    
        present(viewController, animated: true) {
            // closure for when the popover is visible
        }
        return viewController
    }
    

    正如@Britto Thomas 所提到的,你真的不应该在小时候添加你的弹出框,而是展示它。

    2018 年 4 月 9 日更新

    抱歉,我忘了添加这个花絮,这很重要。由于弹出框是为 iPad 设计的,因此您必须按如下方式实现此委托方法:

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
    

    否则它将简单地将其呈现为任何其他模态,尽管我们将“modalPresentationStyle”设置为“.popover”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 2014-08-27
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多