在我的应用程序中,不要使用autolayout和size类。以编程方式创建所有元素。
计算视图宽度并为每个元素设置框架。这是我的代码。
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=@"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(@"ttttt %@",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,(self.view.frame.size.height/2)-210,350, 50)];
AccountLbl.text=@"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-175,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = @"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Username" attributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
[self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-135,self.view.frame.size.width-80, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = @"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
[self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:@selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:@"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), (self.view.frame.size.height/2)-90, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
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,(self.view.frame.size.height/2)-65, self.view.frame.size.width-80, 30.0);
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];
[self.view addSubview:LoginBtn];
如何计算此代码的动态高度。 任何人都可以帮助我。如何计算动态高度。给我任何解决方案。谢谢你。
答案 0 :(得分:2)
在.pch文件中写下
#define iPhoneFactorX ([[UIScreen mainScreen] bounds].size.width*1.00/1080.0)
现在根据屏幕尺寸1080x1920
编写代码E.g。你想设置一个600 * 400的UIImage
UIImageView *m1 = [[UIImageView alloc] initWithFrame:CGRectMake(240*iPhoneFactorX, 100*iPhoneFactorX, 600*iPhoneFactorX, 400*iPhoneFactorX)];
以下是我将起点计算为240
(1080-600)/2
^^^ width of image
这为所有屏幕制作了1个代码......
答案 1 :(得分:1)
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=@"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(@"ttttt %@",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,TitleLbl.frame.origin.y + TitleLbl.frame.size.height + 10,350, 50)];
AccountLbl.text=@"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, AccountLbl.frame.origin.y + AccountLbl.frame.size.height + 10,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = @"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Username" attributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
// [self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(UserNameFld.frame.origin.x, UserNameFld.frame.origin.y + UserNameFld.frame.size.height + 10,UserNameFld.frame.origin.x + UserNameFld.frame.size.width, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = @"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
// [self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:@selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:@"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), PassWorldFld.frame.origin.y + PassWorldFld.frame.size.height + 50, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
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,ForPassBtn.frame.origin.y + ForPassBtn.frame.size.height + 50, self.view.frame.size.width-80, 30.0);
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];
[self.view addSubview:LoginBtn];