ios - Get size of currently visible UIViewController from a UIView -
i've been looking @ same problem long i'm missing simple solution here.
i created small library provide custom uiview sticks keyboard 1 imessage (aka doesn't hide keyboard): https://github.com/oseparovic/messagecomposerview
basically problem i'm experiencing when user init's custom view want view following default rect initialized:
cgfloat defaultheight = 44.0; cgrect frame = cgrectmake(0, [self currentscreensize].height-defaultheight, [self currentscreensize].width, defaultheight)
this requires currentscreensize calculated within uiview. i've tried multiple implementations of have downsides. there doesn't seems solution due breaking principles of mvc.
there lots of duplicate questions on assume have access rest of code base (e.g. app delegate) custom view not i'm looking self contained solution.
here 2 leading implementations i'm using:
nextresponder
this solution seems successful in wide variety of scenarios. next responder's frame conveniently doesn't include nav or status bar , can used position uiview @ bottom of screen.
the main problem self.nextresponder
within uiview nil
@ point of initialization, meaning can't used (at least not know) set initial frame. once view has been initialized , added subview though seems work charm various repositioning uses.
- (cgsize)currentscreensize { // return screen size respect orientation return ((uiview*)self.nextresponder).frame.size; }
applicationframe
this solution using long time it's far more bulky , has several problems. first of all, using applicationframe have deal nav bar height otherwise offset position of view. means have determine if visible, height , subtract currentsize.
getting nav bar unfortunately means need access uinavigationcontroller not simple accessing uiviewcontroller. best solution i've had far below included currentnavigationbarheight
. found issue though fail nav bar height if uialertview present [uiapplication sharedapplication].keywindow.rootviewcontroller
evaluate _uialertshimpresentingviewcontroller
- (cgsize)currentscreensize { // there few problems implementation. namely nav bar height // unreliable. example when uialertview height present // couldn't determine nav bar height. above method appears // working more consistently. if doesn't work try method below instead. return [self currentscreensizeininterfaceorientation:[self currentinterfaceorientation]]; } - (cgsize)currentscreensizeininterfaceorientation:(uiinterfaceorientation)orientation { // http://stackoverflow.com/a/7905540/740474 // size of application frame (screensize - status bar height) cgsize size = [uiscreen mainscreen].applicationframe.size; // if orientation @ point landscape hasn't rotated yet use landscape size instead. // handling differs between ios 7 && 8 need check if size configured or not. on // ios 7 height still greater width in landscape without call on ios 8 // won't if (uiinterfaceorientationislandscape(orientation) && size.height > size.width) { size = cgsizemake(size.height, size.width); } // subtract height of navigation bar screen height size.height -= [self currentnavigationbarheight]; return size; } - (uiinterfaceorientation)currentinterfaceorientation { // returns orientation of interface not device. 2 not happen in exact unison // point important. return [uiapplication sharedapplication].statusbarorientation; } - (cgfloat)currentnavigationbarheight { // todo fail correct height when uialertview present id nav = [uiapplication sharedapplication].keywindow.rootviewcontroller; if ([nav iskindofclass:[uinavigationcontroller class]]) { uinavigationcontroller *navc = (uinavigationcontroller *) nav; if(navc.navigationbarhidden) { return 0; } else { return navc.navigationbar.frame.size.height; } } return 0; }
does have suggestion how can best calculate uiviewcontroller size within uiview. i'm totally open other suggestions on how stick uiview bottom of screen upon initialization may have overlooked. thank you!
+ (id) getcurrentuiviewcontroller : (id)res { if([res iskindofclass:[uiviewcontroller class]]) { return res; } else if ([res iskindofclass:[uiview class]]) { return [function getcurrentuiviewcontroller:[res nextresponder]]; } else { return nil; } }
Comments
Post a Comment