2015-06-19 31 views
4

我有tableview,並且我爲它創建了「detailview」的自定義xib uiview。點擊桌面單元格時,我想在滾動區域的中心顯示此詳細視圖。我可以展示這個觀點,但不能集中它。當我將值設置爲手動框架時,子視圖將位於中心(大約),但是當我點擊底部的單元格時,子視圖出現在頁面的頂部,並且當我滾動tableview時它也在移動。如何在tableview的中心創建xib子視圖swift

請幫我在滾動區域的中心顯示此視圖和固定

這裏是我的代碼;

詳情查看:

class TopTenDetailView: UIView { 

     var screenWidth:CGFloat = UIScreen.mainScreen().bounds.width*0.08 
     var screenHeight :CGFloat = UIScreen.mainScreen().bounds.height*0.08 

     class func instanceFromNib() -> UIView { 
      return UINib(nibName: "TopTenDetail", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView 
     } 

     override func awakeFromNib() { 
      super.awakeFromNib() 
      self.layer.cornerRadius=10 

      let testFrame : CGRect = CGRectMake(screenWidth,screenHeight,320,480) 
      self.frame = testFrame 
      self.userInteractionEnabled=true 
     } 

     @IBAction func close(sender: UIButton) { 
      self.hidden=true 
     } 
} 

而且TableViewController的方法;

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      var detailView = TopTenDetailView.instanceFromNib 
      self.view.addSubview(detailView()) 
     } 

回答

1

這是設置的方式有很多問題,我會很驚訝,如果它實際上按預期工作。

一個更好的,更簡單的安裝使用OverFullScreen演講風格,它是這樣的:

  • 爲您詳細視圖單獨UIViewController,姑且稱之爲DetailViewController使用Interface Builder。確保將背景顏色設置爲CLEAR
  • 從「基準」UIViewController接通一個segue,將UITableView保存爲DetailViewController併爲segue命名。我們稱它爲'detailSegue',基本上從一個視圖控制器拖到另一個視圖控制器。確保您不是從視圖中拖動,而是從視圖控制器頂部的黃色圖標拖動。你在Interface Builder中完成。

enter image description here enter image description here

好了,現在的代碼:

// MARK : - UITableViewDelegate 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     self.performSegueWithIdentifier("detailSegue", sender: self) 
    } 

    // MARK: - segues 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if let vc = segue.destinationViewController as? UIViewController{ 
      vc.modalPresentationStyle = .OverFullScreen 

     } 
    } 

OverFullScreen表現風格採用了適當的UIViewController模式賽格瑞但離開呈現UIViewController可見下提出的一個。

然後,您可以使用Interface Builder和自動佈局在DetailViewController上佈局任何您想要的內容,而無需在運行時對佈局進行哈希匹配計算。

希望它有幫助!

enter image description here enter image description here

+2

就是這麼簡單!非常感謝,它幫助了很多.. – atkafasi

相關問題