I am using UIView subclass to add my detail view in my UIViewController. My UIViewController has on UIView and adding Detail subview to it.
here is my 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 for 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;
}
// My 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 is that when i am hiding the detail view and open detail view in different orientation my view become like this.

the red color is of scrollview. can anyone check this code and let me know what is the problem. Any help will be appreciated.
You need to add the autoResizing flags and mask. Try this:
I suggest you also look at the docs for these properties on UIView and their subclasses. The masks can be used to cause your view to remain in the same relative position to the top or bottom, or not depending on how you use them.