2016-03-31 1 views
-1

私は水平方向にのみや、プログラムですべてをやっ動作するアプリケーションに取り組んでいます。制約の使用方法プログラム的には?

イメージビュー、2つのテキストフィールド、2つのボタンがありますが、これらのテキストフィールドとボタンに制約を設定する方法がわかりません。つまり、iPhone 5で使用しているときには問題ありません。プラスそれは小さく見える。 のおかげで、私はここにいくつかのコードやスクリーンショット

UIImageView *logoImage = [[UIImageView alloc]init]; 
    logoImage.frame = CGRectMake(CGRectGetMidX(self.view.frame)-100, 10, 250, 100); 
    [logoImage setImage:[UIImage imageNamed:@"antya_logo1.png"]]; 

    [self.view addSubview:logoImage]; 

    UILabel *loginLabel = [[UILabel alloc]init]; 
    loginLabel.frame = CGRectMake(10, 0, 100, 40); 
    loginLabel.text = @"Login Form"; 
    loginLabel.textColor = [UIColor blackColor]; 
    loginLabel.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 


    CGFloat Xuser = CGRectGetMidX(self.view.frame)-120; 
    CGFloat Yuser = CGRectGetMaxY(loginLabel.frame)+80; 

    usernameField = [[UITextField alloc]init]; 
    usernameField.frame = CGRectMake(Xuser, Yuser, 300, 35); 
    usernameField.placeholder = @" User Name"; 
    usernameField.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 
    usernameField.backgroundColor = [UIColor whiteColor]; 
    usernameField.layer.cornerRadius = 7; 
    usernameField.layer.borderWidth = 0.5; 

    [self.view addSubview:usernameField]; 

    UIImageView *userImgV = [[UIImageView alloc]init]; 
    userImgV.frame = CGRectMake(CGRectGetMinX(usernameField.frame)-35, CGRectGetMinY(usernameField.frame)+5, 25, 25); 
    [userImgV setImage:[UIImage imageNamed:@"user-icon.png"]]; 

    [self.view addSubview:userImgV]; 

    CGFloat Ypass = CGRectGetMaxY(usernameField.frame)+20; 

    passwordField = [[UITextField alloc]init]; 
    passwordField.frame = CGRectMake(Xuser, Ypass, 300, 35); 
    passwordField.placeholder = @" Password"; 
    passwordField.secureTextEntry = YES; 
    passwordField.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 
    passwordField.backgroundColor = [UIColor whiteColor]; 
    passwordField.layer.cornerRadius = 7; 
    passwordField.layer.borderWidth = 0.5; 


    [self.view addSubview:passwordField]; 

これは私の電話6Sプラス enter image description here

を掲示していますし、これは私が

enter image description here

が私を助けてくださいphone5sでありますVieの上のアイテムの@IBOutletそれぞれを追加した後

+1

あなたはどんな制約を設定しませんでした?最初に何かを試していない場合... –

+0

特定のサイズの使用から制約の使用にコードを切り替える必要があります。ビューのアンカーを見て、彼らは最も使いやすいです。その後、立ち往生したときに戻ってきて、さらに具体的な質問をしてください。最後に、プログラムですべてを実行することは、どのように動作するかを知る良い方法です。しかし、xibやストーリーボードを使用すると、多くのコードと労力を節約できます。特にこのレイアウトがどれくらいシンプルであるかを考えると、 – drekka

+0

は私がどんな制約を使用していないいいえ、私はプログラム的にそれゆえ私は、このアプリケーションを作っています、ここで求めています使用する方法を知らないんだけ水平に –

答えて

1

注:ビューの自動レイアウト設定フレームを扱っている仕事に行くのではありません。そのビューがすべてのiPhoneデバイスで正しく表示されるように制約を設定する必要があります。

UIImageView *logoImage = [[UIImageView alloc] init]; 
[logoImage setImage:[UIImage imageNamed:@"antya_logo1.png"]]; 
logoImage.translatesAutoresizingMaskIntoConstraints = false; 

[self.view addSubview:logoImage]; 

UILabel *loginLabel = [[UILabel alloc]init]; 
loginLabel.text = @"Login Form"; 
loginLabel.textColor = [UIColor blackColor]; 
loginLabel.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 
loginLabel.translatesAutoresizingMaskIntoConstraints = false; 

[self.view addSubview:loginLabel]; 

UITextField *usernameField = [[UITextField alloc]init]; 
usernameField.placeholder = @" User Name"; 
usernameField.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 
usernameField.backgroundColor = [UIColor whiteColor]; 
usernameField.layer.cornerRadius = 7; 
usernameField.layer.borderWidth = 0.5; 
usernameField.translatesAutoresizingMaskIntoConstraints = false; 

UIImageView *userImgV = [[UIImageView alloc]init]; 
[userImgV setImage:[UIImage imageNamed:@"user-icon.png"]]; 

//set left view of textfield 
usernameField.leftView = userImgV; 
usernameField.leftViewMode = UITextFieldViewModeAlways; 
[self.view addSubview:usernameField]; 


UITextField *passwordField = [[UITextField alloc]init]; 
passwordField.placeholder = @" Password"; 
passwordField.secureTextEntry = YES; 
passwordField.font = [UIFont fontWithName:@"LaoSangamMN" size:18]; 
passwordField.backgroundColor = [UIColor whiteColor]; 
passwordField.layer.cornerRadius = 7; 
passwordField.layer.borderWidth = 0.5; 
passwordField.translatesAutoresizingMaskIntoConstraints = false; 

//set left view of textfield 

UIImageView *passwordImgV = [[UIImageView alloc]init]; 
[passwordImgV setImage:[UIImage imageNamed:@"password-icon.png"]]; 

passwordField.leftView = passwordImgV; 
passwordField.leftViewMode = UITextFieldViewModeAlways; 
[self.view addSubview:passwordField]; 


UIButton *buttonSignUp = [UIButton buttonWithType:UIButtonTypeCustom]; 
[buttonSignUp setTag:101]; 
[buttonSignUp setTitle:@"SIGNUP" forState:UIControlStateNormal]; 
[buttonSignUp addTarget:self action:@selector(<your selector>) forControlEvents:UIControlEventTouchUpInside]; 
buttonSignUp.translatesAutoresizingMaskIntoConstraints = false; 
[self.view addSubview:buttonSignUp]; 

UIButton *buttonFP = [UIButton buttonWithType:UIButtonTypeCustom]; 
[buttonFP setTag:101]; 
[buttonFP setTitle:@"SIGNUP" forState:UIControlStateNormal]; 
[buttonFP addTarget:self action:@selector(<your selector>) forControlEvents:UIControlEventTouchUpInside]; 
buttonFP.translatesAutoresizingMaskIntoConstraints = false; 
[self.view addSubview:buttonFP]; 

//setting constraints 

//logoImage 
//leading 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]]; 

//Top 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]]; 

//traling 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:logoImage attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]]; 


// usernameField 
//leading 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:usernameField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]]; 

//traling 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]]; 

//top 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]]; 

//passwordField 
//leading 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]]; 

//traling 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]]; 

//top 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]]; 


//buttonSignUp and buttonFP 

//leading for buttonSignUp 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]]; 

//traling for buttonFP 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]]; 

//equal width for both 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]]; 

//space between both btns 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeRight multiplier:1.0 constant:10]]; 

//top for both 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]]; 

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]]; 

//bottom 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]]; 

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]]; 
+0

ありがとう@MGP素晴らしい仕事..... –

0

を進めますwController。 自動レイアウトビジュアル形式言語を使用して、プログラムで制約を実装することができます。私のプロジェクトから理解するためのサンプルコードです。

スウィフトバージョン)セルawakeFromNibかまたはあなたがのviewDidLoadに使用することができますページの場合:Using Autolayout Visual Format with Swift?

前に引用した

avaImg.translatesAutoresizingMaskIntoConstraints = false 
    usernameBtn.translatesAutoresizingMaskIntoConstraints = false 
    infoLbl.translatesAutoresizingMaskIntoConstraints = false 
    dateLbl.translatesAutoresizingMaskIntoConstraints = false 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|-10-[ava(30)]-10-[username]-7-[info]-10-[date]", 
     options: [], metrics: nil, views: ["ava":avaImg, "username":usernameBtn, "info":infoLbl, "date":dateLbl])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-10-[ava(30)]-10-|", 
     options: [], metrics: nil, views: ["ava":avaImg])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-10-[username(30)]", 
     options: [], metrics: nil, views: ["username":usernameBtn])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-10-[info(30)]" 
     , options: [], metrics: nil, views: ["info":infoLbl])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-10-[date(30)]", 
     options: [], metrics: nil, views: ["date":dateLbl])) 

追加のリンク(のObjective-Cバージョン)あなたはhere同じことを行う多くの詳細を見つけることができます。

関連する問題