在我的应用程序中,不要使用自动布局和尺寸类别。以编程方式创建所有元素。
计算 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/