简体   繁体   中英

how do I resize a UIView after orientation change (code includes doesn't seem to work)

Why doesn't this UIView layout code work as I want?

Background:

  • I have a custom UIView I have in my UIViewController
  • The custom UIView has a clock background imageview and an hourhand imageview
  • After introducing some code (see below) to try to resize the hour hand for an orientation change I'm getting stuck.
  • The code below really stuffs things up - the border outlike of the hour glass hand is way off with this code

Any ideas what I'm doing wrong - there's obvious some assumption or misunderstanding I have for it to screw up like this...

Code called by UIViewController (in "didRotateFromInterfaceOrientation" method) to my custom view:

// Centre image 
self.hourHandImageView.center = self.hourFaceUIImageView.center;

// Resize to cater for either Portrait or Landscape orientation
CGSize currSize = self.hourFaceUIImageView.frame.size;
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
CGSize newHourHandViewSize = CGSizeMake(newHourHandImageheightWidth, newHourHandImageheightWidth);
CGRect newRect = self.hourHandImageView.frame;
newRect.size = newHourHandViewSize;
self.hourHandImageView.frame = newRect;

PS. Without the "Resize to cater for either Portrait or Landscape orientation" code the hour hand correctly stays centered where it should (noting I use transformation to turn it around). There all I really need to do is resize the hour hand appropriately after an orientation change, noting the hour background image is smaller in the landscape mode.

First, how can you tell if you are in portrait or landscape orientation? With this (?):

float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;

What i would advice you, is to do something like this:

-

(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        [self reOrderToPortrait];
    } else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        [self reOrderToLandScape];
    }
}

Then on each method, you should define the new frames for your views. Start by removing your autosizing from your views, because you will define the new frames, and you dont want that to interfere with. You could also define some mesures like this:

#define hourHandImageViewPortrait CGPointMake(...,...)
#define hourHandImageViewLandScape CGPointMake(..., ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM