有没有一种方法可以在 Interface Builder 中实际“视觉设计”双视图(横向和纵向)?

发布于 2024-10-20 06:22:16 字数 1533 浏览 8 评论 0原文

我当前的设计要求在数据的纵向和横向视图之间添加一些很酷的动画。数据表示相当复杂,因此在图形和数据元素之间,屏幕上可能同时有大约 30 个按钮和标签。我创建了一个设计非常紧凑的界面...现在我正在布置横向视图的元素。到目前为止,我发现做到这一点的唯一方法是在代码中实际布局景观视图,例如:

-(void) positionViews {
    UIInterfaceOrientation destOrientation = self.interfaceOrientation;
    if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        //---if rotating into PORTRAIT mode
        timeBGbox.frame = CGRectMake(14, 88, 134, 14);
        targetTime.frame = CGRectMake(0, 99, 150, 29);
        energyBGbox.frame = CGRectMake(156, 88, 68, 14);
        targetEnergy.frame = CGRectMake(154, 99, 70, 29);
        speedBGbox.frame = CGRectMake(234, 88, 68, 14);
        targetSpeed.frame = CGRectMake(226, 99, 48, 29);
        speedLabel.frame = CGRectMake(264, 99, 40, 36);
    } else {
        //---if rotating to LANDSCAPE mode
        timeBGbox.frame = CGRectMake(156, 12, 152, 14);
        targetTime.frame = CGRectMake(160, 23, 150, 29);
        energyBGbox.frame = CGRectMake(156, 50, 68, 14);
        targetEnergy.frame = CGRectMake(154, 61, 70, 29);
        speedBGbox.frame = CGRectMake(234, 50, 74, 14);
        targetSpeed.frame = CGRectMake(228, 61, 48, 29);
        speedLabel.frame = CGRectMake(269, 61, 40, 36);
    }
}

这是一个艰苦的过程,需要进行多次尝试才能完成我在 IB 中几秒钟内可以直观地完成的任务。目前,当我在 IB 中的视图之间切换并在横向模式下手动移动元素时,切换回纵向模式后,它们位于错误的位置。我尽可能创造性地使用了“弹簧和支柱”,但我的大多数元素都需要精确的手动放置,超出了自动调整尺寸的范围。

我的问题是: IB 中是否有切换模式,可以使用可视化布局工具开发视图的两种图形布局,或者数字代码是唯一的方法吗?

My current design calls for some cool animation between portrait and landscape views of the data. The data representation is fairly complex, so between graphical and data elements, there are probably about 30 buttons and labels on screen at once. I've created a very tightly designed interface... now I am laying out the elements for the landscape view. The only way I've found to do this so far is to actually lay out the landscape view in code, for instance:

-(void) positionViews {
    UIInterfaceOrientation destOrientation = self.interfaceOrientation;
    if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        //---if rotating into PORTRAIT mode
        timeBGbox.frame = CGRectMake(14, 88, 134, 14);
        targetTime.frame = CGRectMake(0, 99, 150, 29);
        energyBGbox.frame = CGRectMake(156, 88, 68, 14);
        targetEnergy.frame = CGRectMake(154, 99, 70, 29);
        speedBGbox.frame = CGRectMake(234, 88, 68, 14);
        targetSpeed.frame = CGRectMake(226, 99, 48, 29);
        speedLabel.frame = CGRectMake(264, 99, 40, 36);
    } else {
        //---if rotating to LANDSCAPE mode
        timeBGbox.frame = CGRectMake(156, 12, 152, 14);
        targetTime.frame = CGRectMake(160, 23, 150, 29);
        energyBGbox.frame = CGRectMake(156, 50, 68, 14);
        targetEnergy.frame = CGRectMake(154, 61, 70, 29);
        speedBGbox.frame = CGRectMake(234, 50, 74, 14);
        targetSpeed.frame = CGRectMake(228, 61, 48, 29);
        speedLabel.frame = CGRectMake(269, 61, 40, 36);
    }
}

This is a painstaking process, taking many trials to accomplish what I could do visually in seconds in IB. Currently, when I toggle between views in IB, and manually move elements in landscape mode, upon switching back to portrait, they are in the wrong place. I've used "springs and struts" as creatively as possible, but most of my elements need precise manual placement beyond the realm of AutoSizing.

My question is:
Is there a toggle mode within IB that enables developing two graphical layouts of a view using the visual layout tools, or is numerical code the only way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

对岸观火 2024-10-27 06:22:16

您可以在 nib 文件中创建任意数量的视图。您可以将其中之一称为“纵向视图”(例如,也连接到文件所有者的“视图”出口的视图),将其中之一称为“横向视图”。将它们都定义为 UIViewController 类中的出口:

@property (nonatomic, retain) IBOutlet UIView *portraitView;
@property (nonatomic, retain) IBOutlet UIView *landscapeView;

根据需要在 IB 中布局字段。诀窍是您需要在头文件中将出口加倍:

@property (nonatomic, retain) IBOutlet UIView *myLabel_portrait;
@property (nonatomic, retain) IBOutlet UIView *myLabel_landscape;

并且在代码中您需要使两个版本保持最新。最好是在更新其中之一时同时更新两者。

那么自动旋转的代码非常简单,只需:

if ( switchingToPortrait)
    self.view = self.portraitView;
else
    self.view = self.landscapeView;

You can create an arbitrary number of views in your nib file. You can call one of them "portrait view" (say, the one that is also hooked up to the "view" outlet of File's Owner), and one of them "landscape view". Define them both as outlets in your UIViewController class:

@property (nonatomic, retain) IBOutlet UIView *portraitView;
@property (nonatomic, retain) IBOutlet UIView *landscapeView;

lay out the fields in IB as you like. The trick then is that you need double the outlets in your header file:

@property (nonatomic, retain) IBOutlet UIView *myLabel_portrait;
@property (nonatomic, retain) IBOutlet UIView *myLabel_landscape;

and in your code you need to keep both versions up to date. Best is to just update both of them when you update one of them.

Then the code to autorotate is really easy, just:

if ( switchingToPortrait)
    self.view = self.portraitView;
else
    self.view = self.landscapeView;
月下客 2024-10-27 06:22:16

我能想到的最好的办法是有两个视图,并在 didRotateFromInterfaceOrientation: 中切换它们。

The best I can think of is to have two views, and switch them in didRotateFromInterfaceOrientation:.

空心空情空意 2024-10-27 06:22:16

AFAIK 在 Interface Builder 中没有办法做到这一点。由于视图是从 nib 文件实例化的,因此无论如何您最终都会得到两个实例。

正如开发人员文档中所述,您可以使用layoutSubviews,或者像这里一样在视图控制器中使用自定义方法。

AFAIK there is no way to do this in Interface Builder. Since the views are instantiated from the nib file, you would end up with two instances anyway.

As stated in the developer docs you could use layoutSubviews, or just use a custom method in your view controller like you have here.

围归者 2024-10-27 06:22:16

在界面生成器中创建横向和纵向视图。对于每个方向,您的元素都按照您想要的方式放置。当您对外观感到满意时,请将坐标复制到“positionViews”方法中。

Create a landscape and portrait view in interface builder. For each orientation position your elements just like you want them. When you're happy with the look and feel, copy the coordinates into your "positionViews" method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文