raja saran
raja saran

Reputation: 41

how to set dynamic height for all iphone screen

In my application dn't use autolayout and size classes. Create all the elements programmatically.

Calculate view width and set frame for each element. Here my code.

 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];

How can i calulate the dynamic heigth for this code. Any one can help me. how to calulate dynamic heigth. give any solution for me. Thanks advance.

Upvotes: 0

Views: 74

Answers (2)

Fahim Parkar
Fahim Parkar

Reputation: 31647

Write below in .pch file

#define iPhoneFactorX ([[UIScreen mainScreen] bounds].size.width*1.00/1080.0)

Now write a code as per screen size of 1080x1920

E.g. you want to set a UIImage of size 600*400

UIImageView *m1 = [[UIImageView alloc] initWithFrame:CGRectMake(240*iPhoneFactorX, 100*iPhoneFactorX, 600*iPhoneFactorX, 400*iPhoneFactorX)];

Below is how I calculate starting point as 240

(1080-600)/2
      ^^^ width of image

This make 1 code for all screens....

Upvotes: 2

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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];

Upvotes: 1

Related Questions