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

时间:2016-09-28 12:01:05

标签: ios objective-c iphone

在我的应用程序中,不要使用autolayout和size类。以编程方式创建所有元素。

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

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这两个按钮我无法放置动态高度。

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

3 个答案:

答案 0 :(得分:1)

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

即可

答案 1 :(得分:0)

获取constant.h课程或已经拥有代码可重用的目的。添加此

#define SCREEN_SIZE ([[UIScreen mainScreen] bounds].size)

然后使用SCREEN_SIZE.height& SCREEN_SIZE.width项目中的任何位置,无论屏幕大小如何。 (需要在任何VC中导入时导入constant.h)。您可以通过从SCREEN_SIZE计算x,y坐标或高度,宽度来动态设置任何控件的框架。

我刚尝试了你的代码,发现Reg和Login按钮框架相同,所以它们是重叠的。我已经解决了,这可以帮助你

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, SCREEN_SIZE.width-80, (SCREEN_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];

y = CGRectGetHeight(LoginBtn.frame) + y + 16; // Need to update the y position

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, SCREEN_SIZE.width-80, (SCREEN_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];

enter image description here

快乐编码..

答案 2 :(得分:0)

我没有使用自动布局并以编程方式创建所有内容,通常我在viewController中使用这样的东西:

 SELECT * FROM test.resourcepointswithlookupsandcategorycountandportalpagescount WHERE HostID = 4532;

它很整洁,可以让你重新调整你的观点。你可以使用缩放,但是更好的UI可以让你的元素在各个设备上保持不变的大小,并使用scrollView等进行调整。