ios - ContentInset of scrollView -


this view, left view when keyboard not showing , right want achieve when keyboard showing.

enter image description here

all fields added scrollview set constraints this, field1 topconstraint set scrollview top , field4 bottomconstraint set scrollview bottom. other fields attached 1 below.

as can see right mockup, when keyboard shown want readjust view that. i'm doing is:

func keyboardwillshow(notification: nsnotification) {        self.field2.snp_updateconstraints { make -> void in           make.bottom.equalto(self.field3.snp_top).offset(8)        } 

with adjust large space between field2 , field3, far good. go on set contentinset:

    let contentinset = uiedgeinsetsmake(0, 0,getkeyboardsize(notification).height, 0)      self.scrollview.contentinset = contentinset     self.scrollview.scrollindicatorinsets = contentinset 

but scrollview not adjust way want because field3 still hiding half behind keyboard.

the reason making post don't understand how contentinsets working scrollview , avoid setting contentoffset manually, if thats possible.

there seems problem constraints i'm setting. current constraint field4 set tied view , not scrollview bottom , i'm setting each edge of scrollview tied view.

updates: if set field4 bottom constraint tied scrollview, scrollviews height 0, how can not know size should if before set scrollview big view itself? has confused me..

updates 2 i'm setting scrollview constraints this:

    self.scrollview.snp_makeconstraints { make -> void in         make.top.equalto(self.snp_toplayoutguidebottom)         make.left.right.equalto(self.view)         make.bottom.equalto(self.snp_bottomlayoutguidebottom)     } 

and uidebugger can see height of scrollview same y coordinate has changed 20, can still not scroll in it?? when debugging can set scrollviews contentsize greater frame, means should scrollable, isn't.

update 3 have came understanding field1 , field4 both need attached scrollviews top , bottom. therefore there difference between scrollviews frame , scrollviews contentsize because it's works intending when contentsize > frame scrollview scrollable. big step forward me , weird thing contentinset.

my understanding contentinset specify how view should shrink in either direction. it's i'm doing inset height of keyboard on bottom of scrollview it's not scrolling, when i'm setting top contentinset random negative digit -100 it's scrolls, why behaviour not achieved when bottom inset adjusted?

so solved following:

i made sure field1 , field4 constraints set scrollview, way scrollview automatically able calculate height, , made possible scroll if content outside view. calculated visible frame keyboard top , frame.

   func keyboardwillshow(notification: nsnotification) {      self.field2.snp_updateconstraints { make -> void in          make.bottom.equalto(self.field3.snp_top)     }      self.field3.snp_updateconstraints { make -> void in          make.bottom.equalto(self.field4.snp_top)     }      self.view.layoutifneeded()      let contentinset = uiedgeinsetsmake(0, 0,getkeyboardsize(notification).height + 4, 0)     let visiblerect = uiedgeinsetsinsetrect(self.scrollview.frame, contentinset)     let scrolltooffset = max(0,self.field4.frame.maxy - visiblerect.height)     self.scrollview.setcontentoffset(cgpoint(x: 0, y: scrolltooffset), animated: true)  } 

the key after changing constraints, needed invalidate current layout , layout views again, way calculations updated constraints done correctly.


Comments