代码之家  ›  专栏  ›  技术社区  ›  Benjamin Ortuzar

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

  •  0
  • Benjamin Ortuzar  · 技术社区  · 16 年前

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

    1. 查看TableView时旋转设备。
    2. 单击“信息”按钮。
    3. 旋转设备(InfoView显示为拉伸)。
    4. 单击“信息”按钮(TableView显示为拉伸)

    看来,未添加到超级视图的视图没有正确地调整大小,因为当设备旋转时,它不是复合视图的一部分。是否有任何方法可以正确地自动调整此视图的大小?

    下面是代码示例

    - (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 回复  |  直到 16 年前
        1
  •  0
  •   fearmint    16 年前

    尝试将代码放入viewwillappear:animated。每次您的视图出现时都会调用它。视图加载被调用一次。