iOS中的ViewController顶部出现随机灰色条(swift)

时间:2018-03-27 18:15:48

标签: ios swift xcode

我正在制作一个应用程序,在主屏幕上我从Parse加载多个项目,我看到一个奇怪的灰色条。我不知道这个酒吧来自哪里,因为它没有出现在故事​​板上,我不知道如何解决它。直到最近随机出现它才成为问题。这是ViewController的代码。

import UIKit
import Parse
import ParseUI
import Kingfisher


class HomeTableViewController: PFQueryTableViewController
{
    override func viewDidLoad ()
    {
    super.viewDidLoad()
    setTitle()
    self.navigationController?.isNavigationBarHidden = true
    self.navigationController?.navigationBar.setHeight(0.0)
//        self.tableView.scrollsToTop = true
//        self.tableView.scrollToNearestSelectedRow(at:            UITableViewScrollPosition.bottom, animated: false)
//        self.tableView.scrollToNearestSelectedRow(at:      UITableViewScrollPosition.top, animated: true)
 }

func setTitle()
{
    var parentView = self.parent
    while parent != nil
    {
        if let menu = parentView as? CucuMenuController
        {
//                menu.setTitleForLabel("Cucus")
            menu.setTitleForLabel("")
            break
        }

        parentView = parentView?.parent
    }
}

var firstTime = true
var totalArticles = 0
let appDel = UIApplication.shared.delegate as! AppDelegate

required init!(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    setupHomefeed()
}

func setupHomefeed()
{
    // This runs before didFinishLoadingWithOptions
    self.parseClassName = "Article"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = true
    self.objectsPerPage = 10
    self.loadingViewEnabled = true
}

override func objectsDidLoad(_ error: Error?)
{
    super.objectsDidLoad(error)
    if firstTime
    {
        firstTime = false
        self.loadObjects()
        self.tableView.reloadData()
    }
    else
    {
        PFObject.pinAll(inBackground: self.objects)
    }
}

override func numberOfSections(in tableView: UITableView) -> Int
{
    return 1
}

override func viewDidAppear(_ animated: Bool)
{
    navigationController?.isNavigationBarHidden = false
    if PFUser.current() == nil || PFUser.current()!["name"] == nil
    {
        let viewController = storyboard!.instantiateViewController(withIdentifier: "LoginController")
        UIApplication.shared.keyWindow?.rootViewController = viewController
    }
    else if UserDefaults.standard.bool(forKey: "showDemo")
    {
        self.performSegue(withIdentifier: "detailSegue", sender: self)
    }
    else
    {
        self.loadObjects()
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if self.objects!.count < totalArticles && self.objects!.count > 0
    {
        return self.objects!.count + 1
    }
    return self.objects!.count
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    if indexPath.row == self.objects?.count
    {
        return 70.0
    }
    else
    {
        return 221.5
    }
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.0
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!

    if indexPath.row == self.objects!.count
    {
        return tableView.dequeueReusableCell(withIdentifier: "loadCell") as UITableViewCell!
    }
    if (indexPath.row > self.objects!.count)
    {
        return cell!
    }

    let imageView = cell?.viewWithTag(200) as! UIImageView
    let titleText = cell?.viewWithTag(101) as! UILabel
    let timeLabel = cell?.viewWithTag(102) as! UILabel
    let newImage = cell?.viewWithTag(100) as! UIImageView
    let diffImage = cell?.viewWithTag(300) as! UIImageView
    let scoreLabel = cell?.viewWithTag(301) as! UILabel
    let catLabel = cell?.viewWithTag(103) as! UILabel
    let article = Article(parseData: self.objects![indexPath.row])

    titleText.text = article.title
    timeLabel.text = article.duration
    catLabel.text = article.category.uppercased()

    let hasRead = UserController.hasReadArticle(article.objectId)
    newImage.isHidden = hasRead

    if hasRead
    {
        let score = UserController.getScoreForArticle(article.objectId)
        scoreLabel.text = NSString(format: "YOUR SCORE: %.0f", score) as String
        var performance = "hard"
        if score >= article.idealScore { performance = "easy" }
        else if score * 2 >= article.idealScore { performance = "medium" }

        diffImage.image = UIImage(named: performance + "ScoreTag")

        if UserController.getPlayableForArticle(article.objectId) {
            cell?.isUserInteractionEnabled = true
        }
    }
    else
    {
        scoreLabel.text = NSString(format: "AVG. SCORE: %.0f", article.idealScore) as String
        diffImage.image = UIImage(named: article.difficulty.lowercased() + "ScoreTag")
    }

    //Image
    let resource = ImageResource(downloadURL: article.getImageURL(), cacheKey: article.objectId)
    imageView.kf.setImage(with: resource)

    UIHelper.addShadowToHomeCell((cell?.viewWithTag(1)!)!)
    return cell!
}

var selectedIndex = -1

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if indexPath.row == self.objects!.count
    {
        self.loadNextPage()
    }
    else
    {
        selectedIndex = indexPath.row
        self.performSegue(withIdentifier: "detailSegue", sender: self)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "detailSegue"
    {
        let dest = segue.destination as? ArticlePreviewViewController
        if UserDefaults.standard.bool(forKey: "showDemo")
        {
            dest?.article = DemoArticle().article
        }
        else
        {
            dest?.article = Article(parseData: self.objects![selectedIndex])
        }
    }

    NotificationCenter.default.post(name: Notification.Name(rawValue: "CUCU_START"), object: nil)
}

override func queryForTable() -> PFQuery<PFObject>
{
    let q = PFQuery(className: "Article").whereKey("releaseDate", lessThanOrEqualTo: Date()).whereKey("validated", equalTo: true)

    if firstTime
    {
        q.fromLocalDatastore()
    }
    else
    {
        let mods = PFQuery(className: "Question").whereKey("article", matchesQuery: q)
        mods.findObjectsInBackground
            {
            (objects, err) -> Void in
            PFObject.pinAll(inBackground: objects)
        }
    }
    print(firstTime)
    totalArticles = q.countObjects(nil)
    q.order(byDescending: "createdAt")
    return q
}
}

以下是一些问题的图片:

No grey bar in view controller

Grey bar in simulation

编辑1: 正如评论中所建议的那样,我甚至尝试调试视图层次结构,但我不知道要删除哪个元素以使灰色条消失。

Attempt to debug view hierarchy

Attempt to find source of problem

1 个答案:

答案 0 :(得分:0)

看起来它可能是您的导航栏。你试过这个:

self.navigationController?.setNavigationBarHidden(true, animated: true)

而不是你的声明:

self.navigationController?.isNavigationBarHidden = true

更新#2:

实际上,我刚刚意识到你在viewDidLoad中有这个。将其移至viewWillAppear,我敢打赌它会起作用。

更新#3:

为了反映这里真正的解决方案,有问题的代码在viewDidAppear中:

navigationController?.isNavigationBarHidden = false