ios:如何为所有iphone屏幕设置动态高度

标签 ios objective-c iphone

在我的应用程序中,不要使用自动布局和尺寸类别。以编程方式创建所有元素。

计算 View 宽度并为每个元素设置框架。这是我的代码。

UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,y,100, 50)];
    TitleLbl.text=@"Login";
    TitleLbl.textColor=[UIColor whiteColor];
    TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
    [self.view addSubview:TitleLbl];
    y=y+30;

    UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,y,350, 50)];
    AccountLbl.text=@"Login or Create Your New Account";
    AccountLbl.textColor=[UIColor whiteColor];
    AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
    [self.view addSubview:AccountLbl];
    y=y+40;



UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [LoginBtn addTarget:self action:@selector(aMethod:)
//     forControlEvents:UIControlEventTouchUpInside];
    [LoginBtn setTitle:@"Login" forState:UIControlStateNormal];
    LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,y, self.view.frame.size.width-80, (self.view.frame.size.height/10));
    LoginBtn.layer.cornerRadius = 10;
    LoginBtn.layer.borderWidth=1.0f;
    LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    //LoginBtn.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:LoginBtn];


  UIButton *RegBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [RegBtn addTarget:self action:@selector(aMethod:)
    //     forControlEvents:UIControlEventTouchUpInside];
    [RegBtn setTitle:@"Register Now" forState:UIControlStateNormal];
    RegBtn.frame = CGRectMake(self.view.frame.origin.x+40, y, self.view.frame.size.width-80, (self.view.frame.size.height/10));
    RegBtn.layer.cornerRadius = 10;
    RegBtn.layer.borderWidth=1.0f;
    RegBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    RegBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    RegBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [self.view addSubview:RegBtn];

我如何计算此代码的动态高度。 尤其 UIButton *RegBtn,UIButton *LoginBtn 这两个按钮我无法设置动态高度。

任何人都可以帮助我。如何计算动态高度。给我任何解决方案。提前致谢。

最佳答案

使用 [UIScreen mainScreen].bounds 查找屏幕尺寸,然后相应地动态更改 UI 元素的宽度和高度。例如,您可能希望 RegBtn 比屏幕宽度小 40 像素。为此,您可以简单地将其宽度计算为 [UIScreen mainScreen].bounds.width – 40.0

关于ios:如何为所有iphone屏幕设置动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39746873/

相关文章:

iOS - 使用当天的简写设置 NSDate 以进行本地化

ios - 从地面确定 iPhone 的高度

ios - 有什么方法可以从 Apple Watch 访问心率传感器吗?

javascript - 在 iPhone 上使用 Mobile Safari 时,是否可以在使用 prompt() Javascript 函数时更改键盘类型?

iphone - 我可以将视频从 iPhone 或 iPad 流式传输到服务器吗?

iOS ionic 应用程序状态栏旋转问题

ios - Sinch - 当从 CallKit 接听电话时,callDidEstablish 不会在锁定屏幕上被调用

iphone - 为什么我似乎无法在 iPhone 应用程序中取消归档由 OS X 应用程序归档的对象?

ios - 如何在 Skobbler SDK for iOS 中更改视觉顾问颜色

iphone - 从另一个窗口中打开已经创建的窗口