UIView子类的addsubview不能同时在两个方向上工作

时间:2012-07-07 12:07:34

标签: iphone ios ipad uiview uiviewcontroller

我正在使用UIView子类在我的UIViewController中添加我的详细信息视图。我的UIViewController位于UIView,并向其添加了详细信息subview

这是我的代码。

#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;
}

//我的详细信息视图

#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

我的问题是,当我隐藏详细视图并以不同方向打开详细视图时,我的视图会变成这样。

enter image description here

红色为scrollview。任何人都可以检查此代码,让我知道是什么问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要添加autoResizing标志和掩码。试试这个:

scrlViewMain = [[UIScrollView alloc] init];
scrlViewMain.autoresizesSubviews = YES:
scrlViewMain.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

我建议您在UIView及其子类上查看这些属性的文档。遮罩可用于使视图保持与顶部或底部相同的相对位置,或者取决于您使用它们的方式。