我有一个小的UIView应该总是出现在屏幕的中央。这适用于纵向模式,但不适用于横向模式。
要绘制UIView我使用:
myView.frame = CGRectMake((visibleArea.size.width - MY_VIEW_WIDTH) / 2, (visibleArea.size.height - MY_VIEW_HEIGHT) / 2, MY_VIEW_WIDTH, MY_VIEW_HEIGHT);
myView.autoresizingMask = (
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin
);
可能遗漏的任何想法?
答案 0 :(得分:21)
为什么不试试
myView.center = controller.view.center
答案 1 :(得分:2)
应用是否自动旋转?
我的意思是,你的-[UIViewController shouldAutorotateToInterfaceOrientation:]
方法是什么样的? Info.plist文件中是否有初始和支持的方向?
同时检查父视图autoresizesSubviews
属性。
答案 2 :(得分:1)
也许您不会更改状态栏的方向。如果您不这样做,设备会认为方向仍然是纵向,而不是横向。
答案 3 :(得分:1)
我创建了一个属性,根据设备的方向给出界限。 (这是UIScreen上的一个类别。)
- (CGRect)boundsWithRespectToOrientation
{
CGRect bounds = self.bounds;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && bounds.size.height > bounds.size.width)
bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.height, bounds.size.width);
else if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) && bounds.size.width > bounds.size.height)
bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.height, bounds.size.width);
return bounds;
}