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

Posted

技术标签:

【中文标题】UIView 子类的 addsubview 不能在两个方向上工作【英文标题】:addsubview of UIView subclass not working in both orientation 【发布时间】:2012-07-07 12:07:33 【问题描述】:

我正在使用UIView 子类在我的UIViewController 中添加我的详细视图。我的UIViewControllerUIView 并添加详细信息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。任何人都可以检查此代码并让我知道问题所在。任何帮助将不胜感激。

【问题讨论】:

在详细视图和 UIViewController 中使用 AutoresizesSubviews。 【参考方案1】:

您需要添加自动调整大小标志和掩码。试试这个:

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

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

【讨论】:

感谢您回答悬崖。是正确的,还需要将 IBOutler UIView *viewDetail 的剪辑子视图设置为 true。

以上是关于UIView 子类的 addsubview 不能在两个方向上工作的主要内容,如果未能解决你的问题,请参考以下文章

不能“addSubView”

UIView 子类中的 self.removeFromSuperview()

UIView 的子类没有视图属性

当前uiview的addSubView

在 UIView 中绘制以及 addSubview

在 addSubview 期间奇怪的重复 UIView