Sunday, 15 January 2012

iphone - How to change UIScrollView contentSize after UITextField resigns first responder -



iphone - How to change UIScrollView contentSize after UITextField resigns first responder -

i'm writing iphone app , have uitextfields covered when keyboard appears; hence set uitextfields in uiscrollview, , set myself delegate when the keyboard becomes active, method gets called:

-(void) textfielddidbeginediting:(uitextfield *)textfield { self.myscrollview.contentsize = cgsizemake(self.myscrollview.contentsize.width, 560); [self.myscrollview setcontentoffset:cgpointmake(0, 200) animated:yes]; }

note, making contentsize taller 1 time textfields have been brought focus, user can still scroll.

similarly, when textfield resigns first responder status, method gets called:

-(void) textfielddidendediting:(uitextfield *)textfield { [self.myscrollview setcontentoffset:cgpointmake(0, 0) animated:yes]; self.myscrollview.contentsize = cgsizemake(self.myscrollview.contentsize.width,self.myscrollview.frame.size.height); }

note, 1 time keyboard has been lowered, content visible, there no need scrolling enabled (contentsize = frame.size).

however, problem because setting contentsize right after contentoffset set, setcontentoffset animation not have time completed. instead, animation looks extremely jerky. suggestions?

working uikeyboarddidshownotification , uikeyboardwillhidenotification idea:

step 1: hear 2 notification

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboarddidshow:) name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillbehidden:) name:uikeyboardwillhidenotification object:nil];

step 2: while keyboard did show

- (void)keyboarddidshow:(nsnotification*)notification { cgsize keyboardsize = [[[notification userinfo] objectforkey:uikeyboardframeenduserinfokey] cgrectvalue].size; bool need_resize; // justice if (need_resize) { double offset; // justice [uiview beginanimations:nil context:null]; [uiview setanimationdelegate:self]; [uiview setanimationduration:0.5]; [uiview setanimationbeginsfromcurrentstate:yes]; [self.view setcenter:cgpointmake(self.view.center.x, self.view.center.y - offset]; [uiview commitanimations]; } }

step 3: while keyboard did hide

// in animation code, set view original place [self.view setcenter:cgpointmake(self.view.center.x, self.view.frame.size.height/2)];

this solution not need uiscrollview, adjust view's place, animation, looks great enough.

iphone ios objective-c uitextfield

No comments:

Post a Comment