2017-03-12 4 views
0

私はUITableViewの中でカスタムビューを中心にしようとしていますがうまくいきますが、スクロールするとテーブルビューの中央には更新されません。ここに私のコード:カスタムUIViewをTableViewに配置する方法は?

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 75)) 
    customView.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 250/255, alpha: 1.0) 
    let label = UILabel(frame: CGRect(x: customView.frame.midX, y: customView.frame.midY, width: 150, height: 75)) 
    label.text = "Text" 
    label.textAlignment = .center 
    label.font = UIFont(name: "HelveticaNeue-Light", size: 18) 
    label.textColor = UIColor.white 
    customView.addSubview(label) 
    label.center = customView.center 
    customView.layer.cornerRadius = 4.0 

    self.view.addSubview(customView) 
    customView.center = CGPoint(x: self.tableView.bounds.width/2, y: self.tableView.bounds.height/2) 

    UIView.animate(withDuration: 1.5, animations: { 
     customView.alpha = 0.0 
    }) { (success: Bool) in 
     customView.removeFromSuperview() 
    } 

どのようにこの作品を作るためのアイデア?ありがとう。

+0

このコードでは「自己」とは何ですか?あなたが望んでいないことは起こっていませんか? – matt

答えて

1

customViewをtableViewに追加します。ビューには追加しません。

self.tableView.addSubview(customView) 
関連する問題