0

我正在使用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

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

在此处输入图像描述

红色是scrollview. 任何人都可以检查此代码并让我知道问题所在。任何帮助将不胜感激。

4

1 回答 1

0

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

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

我建议您还查看有关 UIView 及其子类的这些属性的文档。蒙版可用于使您的视图与顶部或底部保持相同的相对位置,或者不取决于您如何使用它们。

于 2012-07-07T13:00:20.757 回答