当视图旋转时移动对象

发布于 2024-11-19 14:44:14 字数 774 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-11-26 14:44:14

看看这个: iphone/ipad 方向处理

您只需根据旋转。

Take a look at this: iphone/ipad orientation handling

You just specify each control location depending on the rotation.

素食主义者 2024-11-26 14:44:14

我知道这可能是一个有点老的问题了,但我最近也遇到了同样的问题。您可能会偶然发现许多建议,例如转换主视图的子视图或其图层。这些都不适合我。

实际上,我发现的唯一解决方案是,既然您希望 UI 控件动态定位,那么不要将它们主要部署在界面生成器中。界面生成器有助于了解纵向和横向动态控件的所需位置。即在界面生成器中创建两个单独的测试视图,一个纵向,另一个横向,根据需要对齐控件,并右下 X、Y、宽度和高度数据,以便与每个控件的 CGRectMake 一起使用。

一旦您从界面生成器中写下所有需要的定位数据,就可以删除那些已经绘制的控件和出口/操作链接。现在他们将不再需要了。

当然,不要忘记实现 UIViewController 的 willRotateToInterfaceOrientation 来设置每次方向变化时控件的框架。

@interface

//Declare your UI control as a property of class.
@property (strong, nonatomic) UITableView *myTable;

@end

@implementation

// Synthesise it
@synthesize myTable

- (void)viewDidLoad
{
    [super viewDidLoad];

// Check to init for current orientation, don't use [UIDevice currentDevice].orientation
  if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 228, 312)];
    }
    else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(78, 801, 307, 183)];
    }
}

    myTable.delegate = self;
    myTable.dataSource = self;

    [self.view addSubview:myTable];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        // Show landscape
        myTable.frame = CGRectMake(20, 20, 228, 312);

    }
    else
    {
        // Show portrait
        myTable.frame = CGRectMake(78, 801, 307, 183);
    }
}

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.

@interface

//Declare your UI control as a property of class.
@property (strong, nonatomic) UITableView *myTable;

@end

@implementation

// Synthesise it
@synthesize myTable

- (void)viewDidLoad
{
    [super viewDidLoad];

// Check to init for current orientation, don't use [UIDevice currentDevice].orientation
  if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 228, 312)];
    }
    else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(78, 801, 307, 183)];
    }
}

    myTable.delegate = self;
    myTable.dataSource = self;

    [self.view addSubview:myTable];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        // Show landscape
        myTable.frame = CGRectMake(20, 20, 228, 312);

    }
    else
    {
        // Show portrait
        myTable.frame = CGRectMake(78, 801, 307, 183);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文