2011-07-13 35 views
2

为什么这个UIView布局代码无法正常工作?如何更改方向更改后的UIView大小(代码包含似乎不起作用)

背景:

  • 我有一个自定义的UIView我在我的UIViewController
  • 自定义的UIView具有时钟背景的ImageView和hourhand ImageView的
  • 引入一些代码后(见下文)尝试调整时针的方向变化我陷入了困境。
  • 下面的代码确实充塞东西 - outlike沙漏手的边界路要走与此代码

任何想法,我做错了什么 - 有明显的一些假设或误会我有它搞砸了这样的... ...通过的UIViewController(在 “didRotateFromInterfaceOrientation” 方法)称为

代码到我的自定义视图:

// 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。如果没有“调整大小以迎合纵向或横向”编码,则时针应正确地保持居中位置(注意我使用转换来转动它)。我真正需要做的是在方向更改后适当调整时针,注意在横向模式下小时背景图像较小。

+0

尺寸变化是否得到反映?定位点是否进入错误的地方? – Ravin

+0

你试过[self.view layoutSubviews]还是[self.view setNeedsDisplay]?尝试setNeedsDisplay刷新视图。在更改框架后在最后调用方法 –

+0

@Ravin - 中心出现在错误的地方,并且大小不正确 – Greg

回答

1

首先,你如何判断你是否在纵向或横向? (?)这一点:

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

什么,我会建议你,是做这样的事情:

-

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

然后在每一个方法,你应该定义为新的帧你的意见。首先从视图中删除自动调整大小,因为您将定义新的框架,并且不希望这样做会干扰。你也可以这样定义一些瑕疵:

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

感谢提示 - 我认为这是我想要在我的代码中完成的工作,即通过使用分配新的Rect进行重置。不要以为你可以看到我正在做的方法有什么问题呢?我有一种感觉,我可以使用你的提示,但我可能以我创建/分配新框架的方式结束相同的问题.... :( – Greg

+0

你的错误乍一看有两个原因:1)您正试图通过比较宽度和高度来判断您是否处于纵向或横向模式。 2)即使你想利用你的意见的自动大小,你仍然在尝试,之后,改变他们的框架。在我看来,它有点火箭科学... – Peres

相关问题