gpt4 book ai didi

ios - 如何将 subview 放在不同屏幕的正确位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:39 25 4
gpt4 key购买 nike

我正在开发一个 ViewController,其中包含一些 UI 元素作为 SubViews,但是当我在不同的设备上运行该应用程序时,我遇到了这些元素的问题。我正在按代码做所有事情。我应该使用 AutoLayout 吗?我应该如何为这些元素使用自动布局?

iphone5 (4 inches)

iphone 4 (3.5 inches)

我有这些元素:

    titleLbl = [[WPCustomLabel alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height/2, 260, 25)];
[titleLbl setFont:[UIFont systemFontOfSize:18]];
[self.view addSubview:titleLbl];

NSLog(@"%f", self.view.bounds.size.height/2);


textLbl = [[WPCustomLabel alloc] initWithFrame:CGRectMake(40, self.view.frame.size.width/2 + 10, 250, 230)];
[textLbl setTextAlignment:NSTextAlignmentJustified];
[self.view addSubview:textLbl];


omitBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-108 , self.view.bounds.size.width/2, 44)];
[omitBtn setTitle:@"Omitir" forState:UIControlStateNormal];
[omitBtn addTarget:self action:@selector(omitInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:omitBtn];


nextBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-108, self.view.frame.size.width/2, 44)];
[nextBtn setTitle:@"Siguiente" forState:UIControlStateNormal];
[nextBtn addTarget:self action:@selector(omitInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextBtn];

最佳答案

如果您正在使用 Storyboard,那么是的,您应该使用自动版式。只需在标签和父 View 的底部之间设置一个约束。

enter image description here

如果您不使用 Storyboard,您可以覆盖 viewWillLayoutSubviews 并根据 View 的大小设置布局。

关于ios - 如何将 subview 放在不同屏幕的正确位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19689925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com