how to create xib subview at the center of tableview swift -


i have tableview , created custom xib uiview "detailview" it. want show detailview @ center of scrolled area when tapped tableview cell. can show view cannot centralized it. when set value frame manually, subview @ center (approximately) when tap cell @ bottom, subview appearing @ top of page , moving when scroll tableview.

please me show view @ center of scrolled area , fixed

here codes;

detail view :

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       } } 

and tableviewcontroller's method ;

      override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {              var detailview = toptendetailview.instancefromnib              self.view.addsubview(detailview())       }  

the way setup has many problems , surprised if ever works intended.

a better, simpler setup uses overfullscreen presentation style , goes this:

  • create separate uiviewcontroller detail view, let's call detailviewcontroller use interface builder. make sure set background color clear
  • wire segue "base" uiviewcontroller holds uitableview detailviewcontroller , give segue name. let's call 'detailsegue' , drag 1 view controller other. make sure not dragging view yellow icon @ top of view controller. done in interface builder.

enter image description here enter image description here

ok, code:

    // 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          }     } 

the overfullscreen presentation style uses proper uiviewcontroller modal segue leaves presenting uiviewcontroller visible under presented one.

you can layout whatever want on detailviewcontroller using interface builder , autolayout without having hacky match calculations on layout @ runtime.

hope helps!

enter image description here enter image description here


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -