在我的应用程序中,不要使用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这两个按钮我无法放置动态高度。
任何人都可以帮助我。如何计算动态高度。给我任何解决方案。谢谢你。
答案 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];
快乐编码..
答案 2 :(得分:0)
我没有使用自动布局并以编程方式创建所有内容,通常我在viewController中使用这样的东西:
SELECT * FROM test.resourcepointswithlookupsandcategorycountandportalpagescount WHERE HostID = 4532;
它很整洁,可以让你重新调整你的观点。你可以使用缩放,但是更好的UI可以让你的元素在各个设备上保持不变的大小,并使用scrollView等进行调整。