2

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

nvrtd frst
  • 6,272
  • 4
  • 28
  • 34
  • for this you have to set frame programmatically. – Pratik Mar 28 '13 at 10:40
  • Was wondering if there was a way to do it with the least amount of code. Would even consider using auto-layout/constraints, if it would solve the problem but I'm not sure it's possible – nvrtd frst Mar 29 '13 at 16:39
  • first of all mention which size you want in all view? in iphone4 & iphone5 landscape and portrait mode. write size of view – Pratik Mar 30 '13 at 03:52
  • The sizes are all listed in the picture for both iPhone 4 and 5 in both portrait and landscape – nvrtd frst Mar 31 '13 at 04:00

3 Answers3

1

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..

David Ben Ari
  • 2,259
  • 3
  • 21
  • 40
  • 1
    Please add some description as to why this is the answer to the question for other readers to understand what is going on here. – Cheesebaron Mar 28 '13 at 11:35
  • Hey David, I see how this could center the view. I could call this on every orientation change. However, to get it to the right size, are you suggesting that I have to set the frame on every rotation as well? – nvrtd frst Mar 29 '13 at 16:37
1

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);
      }
  }
rvijay007
  • 1,357
  • 12
  • 20
0

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.

  • Thanks user, was wondering if there was a way to do it with the least amount of code. Would even consider using auto-layout/constraints, if it would solve the problem but I'm not sure it will. – nvrtd frst Mar 29 '13 at 16:38