How to do a custom layout of a UIView so it works with both iPhone 5 screen dimensions as well as regular screen dimensions

StackOverflow https://stackoverflow.com/questions/15677544

質問

Here is the desired outcome. The blue area is the UIView of interest. The UIView is not a UIImageView.

enter image description here

I've tried all sorts of arrangements with auto-resizing masks to no avail

役に立ちましたか?

解決

This can only be done programmatically. One option is what @user2223761 suggests with subclassing. If you don't want to subclass UIView, then you need to set the frames on orientation changes and set yourView.center to be the center of the center.

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

   if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
          // Make sure that the frame is centered in the screen
          NSInteger paddingLeftSide = (self.view.bounds.size.width - 480) / 2;
          self.view.frame = CGRectMake(paddingLeftSide, 0, 480, 320);   
      } else {
          self.view.frame = CGRectMake(0, 0, 320, 320);
      }
  }

他のヒント

Dealing with different screen sizes can be tricky. In your case it is not :) since you want to center the view in the screen what ever size it is, all you need to do is set the center of the view to be the center of the screen.

CGRect screenBounds = [[UIScreen mainScreen] bounds];
view.center = CGPointMake(screenBounds.size.width/2,screenBounds.size.height/2);

This code assumes the view's superView's bounds is the same size as the screenBounds..

First: Subclass UIView (create a MYUIView). Second: override the method

- (void)layoutSubviews {
   [super layoutSubviews];
   // .. put your code...
}

and perform the frame update manually inside that method by reading the screen size. auto-resize mask must be set to UIViewAutoresizingNone.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top