gpt4 book ai didi

ios - UILabel 的位置以兼容 iPhone 4 和 iPhone 5

转载 作者:行者123 更新时间:2023-11-29 03:59:56 24 4
gpt4 key购买 nike

为我的应用程序制作 UILabel 的简单代码是

UILabel *todaysGame = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 250)];
todaysGame.text = @"Today's Game";
todaysGame.backgroundColor = [UIColor grayColor];
[self.view addSubview:todaysGame];

这对于我的 iPhone 5 屏幕高度来说是完美的,但在 iPhone 4 屏幕上却变得困惑。我尝试阅读 iOS 6 的自动布局功能,但真的不明白如何做到这一点。我不想使用 Storyboard 或 NIB,我想以编程方式执行此操作。如何定位与 iPhone 4 和 5 的屏幕高度兼容的 UI 元素。

我还尝试看看这是否有帮助而不是使用数字

screenBound = [[UIScreen mainScreen] bounds];
screenSize = screenBound.size;
screenWidth = screenSize.width;
screenHeight = screenSize.height;

并在 CGRectMake() 方法中使用“screenHeight”变量。

我使用的是 Xcode 4.6 和 iPhone 5 (iOS 6.1.3) 以及 iPhone 4。

最佳答案

您可以在 UIViewController- (void)viewWillLayoutSubviews 方法中以编程方式设置框架,或设置 View 的 autoresizingMask 属性。

设置框架:

- (void)viewWillLayoutSubviews {
if ([UIScreen mainScreen].bounds.size.height == 568) {
self.label.frame = CGRectMake(0, 0, 320, 100); // for iPhone 5
} else {
self.label.frame = CGRectMake(0, 0, 320, 60);
}
}

或者在- (void)viewDidLoad中设置autoresizingMask:

- (void)viewDidLoad {
[super viewDidLoad];
CGFloat height = self.view.bounds.size.height * 0.2 // 20% of parent view
self.label.frame = CGRectMake(0, 0, 320, height);
self.label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
}

关于ios - UILabel 的位置以兼容 iPhone 4 和 iPhone 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16000400/

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