【问题标题】:frame size issue in UIViewControllerUIViewController 中的帧大小问题
【发布时间】:2012-07-20 04:25:35
【问题描述】:

所以我想添加一个宽度为 450 和屏幕高度的 UIViewController,所以如果它是纵向的,那么高度是 1024,如果是横向,那么高度是 768。这个名为 ProfileViewController 的视图控制器有一个已设置的 xib在 450 和 1024。自动调整大小设置为具有灵活的高度。

 profViewController = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
            self.profileViewController_ = profViewController;
            [profViewController release];

所以在 ProfileViewController 中,我还初始化了另一个名为 FeedsViewController 的视图控制器。现在的问题是,在 profViewController viewDidLoad 中,无论方向如何,高度始终设置为 1024。我怎样才能让它适应我的方向?我已经尝试设置 profViewController 的边界,但是这个更改会在 viewDidLoad 之后反映出来。

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    如果您以与在 NIB 中设置 ProfileViewController 视图的框架相同的方式设置 FeedsViewController 视图的框架,那么事物应该会自动正确调整大小。也就是说,你给它一个你想要的相对于外部视图的初始大小,然后让系统的自动调整大小行为从那里获取东西。

    ProfileViewControllerviewDidLoad 中尝试这样的事情:

    // create and init self.feedsViewController
    // ...
    
    CGRect b = self.view.bounds;
    self.feedsViewController.view.frame = CGRectMake((b.size.width - 450) / 2, 0, 450, b.size.height);
    self.feedsViewController.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    

    【讨论】:

      【解决方案2】:

      要找到高度和宽度,试试这个

      CGRect screenRect = [[UIScreen mainScreen] bounds];
      CGFloat screenWidth = screenRect.size.width;
      CGFloat screenHeight = screenRect.size.height;
      

      【讨论】:

      • 这不是我想要的......我希望它是自动的
      【解决方案3】:

      覆盖profViewController的以下方法

      - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
          [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
      if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
          self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 450, 1024);
      
      }
      else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
          self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 450, 768);
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果你想改变你的tableview框架,你应该在viewDidAppear:而不是viewDidLoad:进行。

        【讨论】:

          猜你喜欢
          • 2011-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多