Self.view.backgroundColor不起作用

时间:2016-07-31 18:31:51

标签: ios swift uiviewcontroller

我有一个SWRevealViewController应用程序。我尝试使用self.view.backgroundColor设置背景颜色失败了。我在下面显示的具体示例是一个UIViewController,它包含一个带有空UIView()页脚的tableView,以便在完成时停止tableView。我正在尝试设置backgroundColor来填充剩余的空间。我的代码如下。

import UIKit

class NewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate {
    @IBOutlet weak var tables: UITableView!

    var parser: NSXMLParser = NSXMLParser()
    var info: [newsarticle] = []
    var postTitle: String = String()
    var postDesc: String = String()
    var eName: String = String()
    var index: Int = Int()
    /*
     // Only override drawRect: if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func drawRect(rect: CGRect) {
     // Drawing code
     }
     */
    var refreshControl: UIRefreshControl!
   override func viewDidLoad() {
    let navicon = UIButton(type: UIButtonType.System)
    navicon.setImage(defaultMenuImage(), forState: UIControlState.Normal)
    navicon.frame = CGRectMake(0, 0, 30, 30)
    let menu = UIBarButtonItem(customView: navicon)
    self.navigationItem.leftBarButtonItem = menu
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    navicon.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), forControlEvents: .TouchUpInside)
        let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/News.xml")!
        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()
    refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(NewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
    tables.separatorColor = UIColor.init(red: 217/255, green: 180/255, blue: 74/255, alpha: 1)
    tables.addSubview(refreshControl)
    //background.frame = self.view.frame
    tables.tableFooterView = UIView()
    self.view.backgroundColor = UIColor.init(red: 241/255, green: 241/255, blue: 242/255, alpha: 1)self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
    func refresh(sender: AnyObject)
    {
        info = [newsarticle]()
        let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/News.xml")!
        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()
        tables.reloadData()
        refreshControl.endRefreshing()
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60;
    }
    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI namespaceURI: String?, qualifiedName qualifiedName: String?, attributes attributeDict: [String : String])
    {
        eName = elementName
        if elementName == "item" {
            postTitle = String()
            postDesc = String()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return info.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell
        if let reusedCell = tableView.dequeueReusableCellWithIdentifier("Cell") {
            cell = reusedCell
        } else {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
        }
        let news: newsarticle = info[indexPath.row]
        if let label = cell.textLabel {
            label.text = news.title
        }
        cell.backgroundColor = UIColor.init(red: 241/255, green: 241/255, blue: 242/255, alpha: 1)
        cell.textLabel!.font = UIFont(name:"Bodoni 72", size: 16)
        cell.textLabel!.textColor = UIColor.init(red: 25/255, green: 149/255, blue: 173/255, alpha: 1)
        return cell    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        if (!data.isEmpty) {
            if eName == "title" {
                postTitle += data
            } else if eName == "description" {
                postDesc += data
            }
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if elementName == "item" {
            let newsart: newsarticle = newsarticle()
            newsart.title = postTitle
            newsart.description = postDesc
            info.append(newsart)
        }
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        index = indexPath.row
        tables.deselectRowAtIndexPath(indexPath, animated:true)
            performSegueWithIdentifier("NewsTransfer", sender:self)
    }
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let Destination: FullNews = segue.destinationViewController as! FullNews
        Destination.info = info[index]

    }




    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

viewDidLoad方法中,您有:

tables.tableFooterView = UIView()

请改为尝试:

tables.tableFooterView = UIView(frame: CGRectZero)

另外,对于下一行(背景颜色),请尝试:

self.view.window!.backgroundColor = UIColor(red: 241/255, green: 241/255, blue: 242/255, alpha: 1.0)