swift - moving subview - offset amount wrong, but visually correct? -


i wrote sample program try , figure out going on. have textfield upon clicking, shows subview instead of showing keyboard. in subview, there dismiss button shifts subview top aligns bottom of screen.

just can build subview in storyboard, subview aligned bottom edge of screen , hidden @ startup. viewdidload calls hidesubview shift offscreen before starts.

i have println before , after each shift tell me position of subview.

the problem have numbers don't add up. tell swift offset y 210, somehow shifts 68.5 not bottom of screen (note not done setting subview inputview of textfield). yet when click on textfield, subview animates bottom up, not midway point. can explain why either of these 2 things happening? 1 other thing - 457.0 initial miny come from? checked initial height of subview strangely 143 (not 141.5).

console output (// - lines put in me clarify)

//hidesubview called viewdidload before. miny:457.0 screenheight:667.0 offset:210.0 after. miny:525.5 screenheight:667.0 offset:210.0  //note:457+210 667, not 525.5 //1st showsubview called clicking on mytextfield before. miny:525.5 screenheight:667.0 subview height:141.5 after. miny:525.5 subview height:141.5 //hidesubview called clicking on dismiss button before. miny:525.5 screenheight:667.0 offset:141.5 after. miny:667.0 screenheight:667.0 offset:141.5  //2nd showsubview called clicking on mytextfield before. miny:667.0 screenheight:667.0 subview height:141.5 after. miny:525.5 subview height:141.5 //hidesubview called clicking on dismiss button before. miny:525.5 screenheight:667.0 offset:141.5 after. miny:667.0 screenheight:667.0 offset:141.5 

main.storyboard screen capture of storyboard viewcontroller.swift

import uikit  class viewcontroller: uiviewcontroller, uitextfielddelegate {    @iboutlet weak var mytextfield: uitextfield!   @iboutlet weak var mysubview: uiview!    var screensize = uiscreen.mainscreen().bounds   var subviewisvisible = false     override func viewdidload() {     super.viewdidload()     mytextfield.delegate = self     hidesubview()   }    override func didreceivememorywarning() {     super.didreceivememorywarning()   }    @ibaction func dismissbuttonclicked(sender: anyobject) {     if subviewisvisible {       hidesubview()     }   }    func textfielddidbeginediting(textfield: uitextfield) {     mytextfield.endediting(true)     if !subviewisvisible {       showsubview()     }   }    func hidesubview () {     var offsetamount = screensize.height - mysubview.frame.miny     println("before. miny:\(mysubview.frame.miny) screenheight:\(screensize.height) offset:\(offsetamount)")     uiview.animatewithduration(0.25, delay: 0.0, options: uiviewanimationoptions.curveeaseout , animations:       {         self.mysubview.frame.offset(dx: 0, dy: offsetamount)       }, completion: {_ in         self.subviewisvisible = false         self.mysubview.hidden = true         println("after. miny:\(self.mysubview.frame.miny) screenheight:\(self.screensize.height) offset:\(offsetamount)")       }     )   }    func showsubview () {     mysubview.hidden = false     println("before. miny:\(mysubview.frame.miny) screenheight:\(screensize.height) subview height:\(mysubview.frame.height)")     uiview.animatewithduration(0.25, delay: 0.0, options: uiviewanimationoptions.curveeaseout , animations:       {         self.mysubview.frame.offset(dx: 0, dy: -self.mysubview.frame.height)       }, completion: {_ in         self.subviewisvisible = true         println("after. miny:\(self.mysubview.frame.miny) subview height:\(self.mysubview.frame.height)")       }     )   } } 

edit:

constraints of mysubview: (easier see comment bannings) constraints on mysubview

the 457 , 143 comes storyboard this:

enter image description here

this behavior happens because view's origin incorrect in viewdidload method. although had bottom constraint bottom layout guide, however, frame hasn't been adjusted yet.

so, if set view's y 500 getting log below:

enter image description here

before. miny:500.0 screenheight:667.0 offset:167.0 after. miny:524.5 screenheight:667.0 offset:167.0 

your mysubview's frame correct after viewdidload has been called autolayout.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -