当视图旋转时移动对象
我有一个 iPad 应用程序,我想以横向方式而不是纵向方式工作。我以编程方式将图像、标签和按钮放入我的视图中,并使用 CGRectMake (x,x,x,x) 告诉他们在视图的中心位置。当应用程序水平旋转时,我需要将标签和按钮向上移动(因为它们在横向模式下无法向下移动那么远),但保持在中心。这是我一直在玩的一些代码:
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(384-(fieldWidth/2)-30,controlTop+45,120,40)]; //these dimensions aren't correct, though they don't matter here
lblDate.text = @"Date:";
lblDate.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDate];
} else {
//the orientation must be portrait or portrait upside down, so put duplicate the above code and change the pixel dimensions
}
感谢您的帮助!
I have an iPad app that I would like to work in the sideways orientation instead of just portrait. I have programatically placed images, labels, and buttons into my view and used CGRectMake (x,x,x,x) to tell them where to go on the view into the center. When the app rotates horizontally, I need my labels and buttons to shift up (since they can't go down as far when in landscape mode), but stay in the center. Here is some code I've been playing with:
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(384-(fieldWidth/2)-30,controlTop+45,120,40)]; //these dimensions aren't correct, though they don't matter here
lblDate.text = @"Date:";
lblDate.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDate];
} else {
//the orientation must be portrait or portrait upside down, so put duplicate the above code and change the pixel dimensions
}
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个: iphone/ipad 方向处理
您只需根据旋转。
Take a look at this: iphone/ipad orientation handling
You just specify each control location depending on the rotation.
我知道这可能是一个有点老的问题了,但我最近也遇到了同样的问题。您可能会偶然发现许多建议,例如转换主视图的子视图或其图层。这些都不适合我。
实际上,我发现的唯一解决方案是,既然您希望 UI 控件动态定位,那么不要将它们主要部署在界面生成器中。界面生成器有助于了解纵向和横向动态控件的所需位置。即在界面生成器中创建两个单独的测试视图,一个纵向,另一个横向,根据需要对齐控件,并右下 X、Y、宽度和高度数据,以便与每个控件的 CGRectMake 一起使用。
一旦您从界面生成器中写下所有需要的定位数据,就可以删除那些已经绘制的控件和出口/操作链接。现在他们将不再需要了。
当然,不要忘记实现 UIViewController 的 willRotateToInterfaceOrientation 来设置每次方向变化时控件的框架。
I know this might be a bit of an old question now looking to the date, but I just very recently faced the same problem. You could stumble upon many suggestions such as transforming main view's subviews or it's layers. Non of this worked for me.
Actually the solitary solution I've found is that since you want your UI controls to be located dynamically then don't deploy them mainly in the interface builder. The interface builder can be helpful knowing the desired locations for dynamic controls in both portrait and landscape orientations. i.e make two separate test views in the interface builder, one portrait and the other landscape, align your controls as you wish and right down X, Y, Width and Height data just to use with CGRectMake for each control.
As soon as you write down all needed positioning data from the interface builder get rid of those already drawn controls and outlets/actions links. They will be of no need now.
Of course don't forget to implement UIViewController's willRotateToInterfaceOrientation to set control's frame with each orientation change.