【问题标题】:Getting a black background when I segue to a new View Controller in Xcode 8?当我在 Xcode 8 中切换到新的视图控制器时得到黑色背景?
【发布时间】:2017-03-20 21:36:08
【问题描述】:

我有 2 个视图控制器,ShipmentsOfResourcesVC 和 PopupVC。 ShipmentsOfResourcesVC 是主视图控制器,它是一个包含多个单元格的表格视图。当你按下一个单元格时,它应该会转到 PopupVC。

segue 工作正常,PopupVC 显示正确,但我遇到的问题是当 PopupVC 出现时,背景是透明的一秒钟然后变黑?我在下方提供了一个链接,指向 YouTube 上演示该问题的 15 秒短片。

Video

在我第一次创建 segue 之前,这不是问题。 PopupVC 将以标准的白色背景出现。但是,我确实开始尝试更改 PopupVC 的背景,例如使其透明并摆弄模糊和活力效果,但我删除了所有这些代码,我仍然得到这个黑色背景。还要确保在 Storyboard 中检查视图的背景颜色设置为白色。我已经尝试删除 segue 并使用不同的名称创建一个新的。我删除了手机上的应用程序并重新安装它,这发生在我的设备和模拟器上。有任何想法吗?可能的错误?在 Xcode 8.2.1 中使用 Swift 3。

ShipmentsOfResourcesVC

import UIKit

class ShipmentsOfResourcesVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var falloutCompanionLabel: UILabel!

var resourceList = ResourceList().listOfResources()
var numberOfResources = ResourceList().numberOfResources()


override func viewDidLoad() {
    super.viewDidLoad()

    myTableView.delegate = self
    myTableView.dataSource = self
    myTableView.backgroundImage()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if let destination = segue.destination as? PopupVC {

            destination.name = resourceList[myTableView.indexPathForSelectedRow!.row]
        }
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return numberOfResources
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath)
    let row = indexPath.row
    cell.textLabel?.text = resourceList[row]

    cell.textLabel!.textAlignment = .center
    cell.textLabel!.textColor = UIColor.darkGray

    return cell
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.deselectRow(at: indexPath, animated: true)

 }
     }

PopupVC

import UIKit

class PopupVC: UIViewController {

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var vendorLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var map: UIImageView!
@IBOutlet weak var popupView: UIView!

var name = ""

override func viewDidLoad() {
    super.viewDidLoad()


    popupView.layer.cornerRadius = 20

    print("The name is \(name)")
    nameLabel.text? = name

    switch name {

    case "Acid":
        vendorLabel.text = "Vendor: Kay"
        locationLabel.text = "Location: Bunker Hill"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Adhesive":
        vendorLabel.text = "Vendor: Daisy"
        locationLabel.text = "Location: Goodneighor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Aluminum":
        vendorLabel.text = "Vendor: Arturo"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 50"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Antiseptic":
        vendorLabel.text = "Vendor: Doctor Sun"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Asbestos":
        vendorLabel.text = "Vendor: Trashcan Carla"
        locationLabel.text = "Location: Sanctuary Hills/Bunker Hill"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Ballistic Fiber":
        vendorLabel.text = "Vendor: KLEO"
        locationLabel.text = "Location: Goodneighor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Ceramic":
        vendorLabel.text = "Vendor: Myrna/Percy"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Circuitry":
        vendorLabel.text = "Vendor: Daisy"
        locationLabel.text = "Location: Goodneighor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Cloth":
        vendorLabel.text = "Vendor: Connie"
        locationLabel.text = "Location: Abernathy Farm"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Concrete":
        vendorLabel.text = "Vendor: Connie"
        locationLabel.text = "Location: Abernathy Farm"
        quantityLabel.text = "Quantity: 50"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Copper":
        vendorLabel.text = "Vendor: Arturo"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Cork":
        vendorLabel.text = "Vendor: Moe"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Crystal":
        vendorLabel.text = "Vendor: Daisy"
        locationLabel.text = "Location: Goodneighor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Fertilizer":
        vendorLabel.text = "Vendor: Connie"
        locationLabel.text = "Location: Abernathy Farm"
        quantityLabel.text = "Quantity: 50"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Fiber Optics":
        vendorLabel.text = "Vendor: Proctor Teagan"
        locationLabel.text = "Location: The Prydwen"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Fiberglass":
        vendorLabel.text = "Vendor: Deb or Lucas"
        locationLabel.text = "Location: Bunker Hill"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Gears":
        vendorLabel.text = "Vendor: Arturo"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Glass":
        vendorLabel.text = "Vendor: Doctor Sun"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 50"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Gold":
        vendorLabel.text = "Vendor: Deb"
        locationLabel.text = "Location: Bunker Hill"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Lead":
        vendorLabel.text = "Vendor: KLEO"
        locationLabel.text = "Location: Goodneighor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Leather":
        vendorLabel.text = "Vendor: Connie"
        locationLabel.text = "Location: Abernathy Farm"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Nuclear Material":
        vendorLabel.text = "Vendor: Alexis"
        locationLabel.text = "Location: Vault 81"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Oil":
        vendorLabel.text = "Vendor: KLEO"
        locationLabel.text = "Location: Goodneighbor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Plastic":
        vendorLabel.text = "Vendor: Myrna/Percy"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Rubber":
        vendorLabel.text = "Vendor: Doctor Sun"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Silver":
        vendorLabel.text = "Vendor: Daisy"
        locationLabel.text = "Location: Goodneighbor"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Springs":
        vendorLabel.text = "Vendor: Myrna/Percy"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Screws":
        vendorLabel.text = "Vendor: Arturo"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 25"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Steel":
        vendorLabel.text = "Vendor: Myrna/Percy"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 100"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    case "Wood":
        vendorLabel.text = "Vendor: Moe"
        locationLabel.text = "Location: Diamond City"
        quantityLabel.text = "Quantity: 100"
        map.image = #imageLiteral(resourceName: "Goodneighbor.gif")
    default:
        print("That item doesn't exist.")
    }
}

@IBAction func xButton(_ sender: Any) {

    dismiss(animated: true, completion: nil)
}
  }

【问题讨论】:

    标签: ios swift swift3 segue


    【解决方案1】:

    检查 PopupVC 上的背景颜色。它可能是透明的或黑色的。如果您想清楚并显示其背后的现有视图控制器,那么您需要将演示样式更改为“在当前上下文中”。

    How to present a modal atop the current view in Swift

    【讨论】:

      【解决方案2】:

      不要把故事板弄得太乱;在我看来,这可能非常令人困惑。我个人不再使用它们,但将它们放在那里也是一个不错的选择,使用它们但通过代码进行设计:
      在您的情况下,在 viewDidLoad 方法中放入呈现的视图控制器这一行: self.view.backgroundColor = .white= UIColor.white 或任何你想要的颜色:-)
      通过代码设置它,您还可以通过编程等方式更改它等等...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        相关资源
        最近更新 更多