iphone:旋转可见视图时旋转翻转视图

时间:2009-10-17 16:32:48

标签: iphone

旋转具有两个视图的视图控制器时,我有一个调整大小的问题,我使用翻转动画切换。 如果我执行以下步骤,则会出现此问题:

  1. 查看桌面视图时旋转设备。
  2. 点击信息按钮。
  3. 旋转设备(infoView显示为拉伸)。
  4. 点击信息按钮(桌面视图显示为拉伸)
  5. 看起来未添加到superview的视图没有正确调整大小,因为它在旋转设备时不是复合视图的一部分。有没有办法让这个视图自动调整大小正确?

    以下是代码示例

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //background image
        backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-app.png"]];
        backgroundImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
        [self.view addSubview: backgroundImageView];
        [backgroundImageView release];
    
    
        //infoView
        aboutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"About.png"]];
        aboutImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
    
        //tableView
        self.tableView = [[[UITableView alloc ] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease ];
        //set the rowHeight once for performance reasons.
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 75;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);                    
    
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.autoresizesSubviews = YES;
    
        //set the rowHeight once for performance reasons.
        [self.view addSubview: tableView];
        //[tableView release];
    
        [self.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];//for resizing on rotation
    
        //info button
        UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
        infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
        infoDarkButtonType.backgroundColor = [UIColor clearColor];
        [infoDarkButtonType addTarget:self action:@selector(infoAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *buttonInfo = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
        self.navigationItem.rightBarButtonItem = buttonInfo;
        [infoDarkButtonType release];   
        self.navigationItem.rightBarButtonItem = buttonInfo;
        [buttonInfo release];   
    
    }
    
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return YES;
    
    }
    
    
    - (void)infoAction:(id)sender{ 
        NSLog(@"Clicked on the info button");
    
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.75];     /* Sub. duration here */
    
        UIView *superview;
        if ((superview = [tableView superview])) {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:superview cache:YES];
            [tableView removeFromSuperview];
            [superview addSubview:aboutImageView];
        } else if ((superview = [aboutImageView superview])) {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
            [aboutImageView removeFromSuperview];
            [superview addSubview:tableView];
        }
    
        [UIView commitAnimations];
    }
    

    由于

1 个答案:

答案 0 :(得分:0)

尝试将代码放在viewWillAppear:animated中。每当您的视图出现时都会调用它。视图确实加载被调用一次。