iphone - addsubview of UIView subclass not working in both orientation -
i using uiview
subclass add detail view in uiviewcontroller
. uiviewcontroller
has on uiview
, adding detail subview
it.
here code.
#import <uikit/uikit.h> #import "appdelegate.h" #import "detailview.h" @interface viewcontroller : uiviewcontroller{ appdelegate *appdelegate; iboutlet uilabel *lbladd; iboutlet uiview *viewdetail; detailview *viewdetailfinal; } @property (nonatomic, retain) detailview *viewdetailfinal; -(ibaction) show; -(ibaction) viewdetailhide; @end #import "viewcontroller.h" #define detailportraitwidth 478 #define detailportraitheight 899 #define detaillandscapewidth 733 #define detaillandscapeheight 642 #define viewdetailheaderheight 150 @implementation viewcontroller @synthesize viewdetailfinal; - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations viewdetailfinal.curorientation = [uidevice currentdevice].orientation; return yes; } - (void)didrotatefrominterfaceorientation:(uiinterfaceorientation)frominterfaceorientation{ bool isportrait = uideviceorientationisportrait(self.interfaceorientation); if (isportrait) { if (!viewdetail.ishidden) { [viewdetailfinal selfsetframe:cgrectmake(0, viewdetailheaderheight, detailportraitwidth , detailportraitheight-viewdetailheaderheight)]; [viewdetailfinal loadcommonview]; } } else{ if (!viewdetail.ishidden) { [viewdetailfinal selfsetframe:cgrectmake(0, viewdetailheaderheight, detaillandscapewidth , detaillandscapeheight-viewdetailheaderheight)]; [viewdetailfinal loadcommonview]; } } } -(ibaction) show{ bool isportrait = uideviceorientationisportrait(self.interfaceorientation); if (isportrait) { if (viewdetail.ishidden) { viewdetail.hidden=no; [self.view addsubview:viewdetail]; viewdetailfinal = [[detailview alloc] initwithframe:cgrectmake(0, viewdetailheaderheight, detailportraitwidth, detailportraitheight-viewdetailheaderheight)]; viewdetailfinal.curorientation = uideviceorientationportrait; [viewdetail addsubview:viewdetailfinal]; [viewdetailfinal loadcommonview]; } else{ viewdetailfinal.curorientation = uideviceorientationportrait; [viewdetailfinal selfsetframe:cgrectmake(0, viewdetailheaderheight, detailportraitwidth, detailportraitheight-viewdetailheaderheight)]; } } else{ if (viewdetail.ishidden) { viewdetail.hidden=no; [self.view addsubview:viewdetail]; viewdetailfinal = [[detailview alloc] initwithframe:cgrectmake(0, viewdetailheaderheight, detaillandscapewidth , detaillandscapeheight-viewdetailheaderheight)]; viewdetailfinal.curorientation = uideviceorientationlandscapeleft; [viewdetail addsubview:viewdetailfinal]; [viewdetailfinal loadcommonview]; } else{ viewdetailfinal.curorientation = uideviceorientationlandscapeleft; [viewdetailfinal selfsetframe:cgrectmake(0, viewdetailheaderheight, detaillandscapewidth, detaillandscapeheight-viewdetailheaderheight)]; } } } -(ibaction) viewdetailhide{ [viewdetailfinal releasememory]; viewdetail.hidden=yes; }
// detail view
#import <uikit/uikit.h> @interface detailview : uiview{ uiscrollview *scrlviewmain; uideviceorientation curorientation; } @property uideviceorientation curorientation; @property (nonatomic, retain) uiscrollview *scrlviewmain; -(void) selfsetframe:(cgrect)frame; -(void) releasememory; -(void) loadcommonview; @end #define scrlviewmainx 0 #define scrlviewmainy 0 #define scrlviewmainwidth 468 #define scrlviewmainportheight 749 #define scrlviewmainlandheight 492 @implementation detailview @synthesize scrlviewmain; @synthesize curorientation; - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { // initialization code [self setframe:frame]; scrlviewmain = [[uiscrollview alloc] init]; } return self; } -(void) selfsetframe:(cgrect)frame{ [self setframe:frame]; if (curorientation == uideviceorientationportrait || curorientation == uideviceorientationportraitupsidedown) { [self.scrlviewmain setframe:cgrectmake(scrlviewmainx, scrlviewmainy, scrlviewmainwidth, scrlviewmainportheight)]; } else{ [self.scrlviewmain setframe:cgrectmake(scrlviewmainx, scrlviewmainy, scrlviewmainwidth, scrlviewmainlandheight)]; } [self.scrlviewmain setcontentsize:cgsizemake(scrlviewmainwidth, 1500)]; } -(void) loadcommonview { if (curorientation == uideviceorientationportrait || curorientation == uideviceorientationportraitupsidedown) { [self.scrlviewmain setframe:cgrectmake(scrlviewmainx, scrlviewmainy, scrlviewmainwidth, scrlviewmainportheight)]; } else{ [self.scrlviewmain setframe:cgrectmake(scrlviewmainx, scrlviewmainy, scrlviewmainwidth, scrlviewmainlandheight)]; } [self.scrlviewmain setbackgroundcolor:[uicolor redcolor]]; [self addsubview:scrlviewmain]; } -(void) releasememory{ [scrlviewmain release]; [self release]; } @end
my problem when hiding detail view , open detail view in different orientation view become this.
the red color of scrollview
. can check code , let me know problem. appreciated.
you need add autoresizing flags , mask. try this:
scrlviewmain = [[uiscrollview alloc] init]; scrlviewmain.autoresizessubviews = yes: scrlviewmain.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight;
i suggest @ docs these properties on uiview , subclasses. masks can used cause view remain in same relative position top or bottom, or not depending on how use them.
Comments
Post a Comment