2016-05-27 10 views
7

これは私をナットにしています。私はどこにでも見てきましたが、これを理解することはできません。確認してください...イメージでUIButtonの高さと幅を設定するには

let findMeButton = UIButton(type: UIButtonType.System) 

    findMeButton.translatesAutoresizingMaskIntoConstraints = false 

    findMeButton.setImage(UIImage(named: "locateMe"), forState: UIControlState.Normal) 

    findMeButton.addTarget(self, action: #selector(MapViewController.findUserLocation(_:)), forControlEvents: UIControlEvents.TouchUpInside) 

    view.addSubview(findMeButton) 

    // I added this line of code and it still doesn't work. 
    findMeButton.frame.size = CGSizeMake(50, 50) 

    findMeButton.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -10).active = true 

    findMeButton.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 5).active = true 

私はまだiOSを学んでいます。イメージでこのUIButtonの高さと幅をどのように設定しますか?私が試したことはすべて私にエラーを与えているか、まったくうまくいきませんでした。私はまだ翻訳するものの周りを頭を悩まそうとしています.AutoMaskIntoConstraintsAutoresizingMaskInto。私はちょうどそれがどこにボタンを持っているが、そのサイズを変更したい(高さ&幅)。

おかげで、事前

EDITに:私は窓の下と右余白にボタンを配置する場合は、この

// Locate user button 
    let locateButton = UIButton(type: UIButtonType.System) as UIButton 

    locateButton.frame = CGRectMake(0, 0, 50, 50) 

    locateButton.setBackgroundImage(UIImage(named: "locateMe"), forState: UIControlState.Normal) 

    locateButton.addTarget(self, action: #selector(MapViewController.findUserLocation(_:)), forControlEvents: UIControlEvents.TouchUpInside) 

    view.addSubview(locateButton) 

にコードを少し変更しました。私はまた、イメージの高さを50 x 50の高さに設定したいと思います。これはどのようにして達成できますか?

編集2:私はあなたがそうするために自動レイアウトを使用しなければならないと思いますが、誰かが私に方法を示すことができます。私がしたことはすべてうまくいっていません。

+0

設定された画像は、何罰金 –

+0

作業を開始していますあなたはそれを意味しますか? – Chris

+0

その画像をボタンの背景画像として設定する必要があります。 –

答えて

8

でボタンの位置とサイズを指定することができますので、ここで私は、ビューの上にボタンを追加するためのコードを書きました。背景画像で

 let button = UIButton(type: UIButtonType.System) as UIButton 
     // set the frame 
     button.frame = CGRectMake(100, 100, 100, 50) 
     // add image 
     button.setBackgroundImage(UIImage(named:"SearchIcon"), forState: UIControlState.Normal) 
     // button title 
     button.setTitle("Test Button", forState: UIControlState.Normal) 
     // add action 
     button.addTarget(self, action: #selector(RootViewController.updateView), forControlEvents: UIControlEvents.TouchUpInside) 

     button.translatesAutoresizingMaskIntoConstraints = false 
    // add button on view 
     self.view.addSubview(button) 
// all constaints 
    let widthContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200) 
    let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100) 
     let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 
     let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) 
     NSLayoutConstraint.activateConstraints([heightContraints,widthContraints,xContraints,yContraints]) 
+0

はい、私はそれを行う方法はわかっていますが、自動レイアウトを使用してボトムと右の余白に固定したいと思います。私はイメージのサイズを設定することはできませんことを行う。 – Chris

+0

OK、下、水平、中央、幅、高さの自動レイアウトを追加するよりも –

+0

私にコードを表示できますか?私もそれを試して、それは私にエラーを与える。そして、申し訳ありませんが、私はこれで初心者です。 – Chris

4

は単に

findMeButton.frame.size = CGSizeMake(width, height) 

でボタンのサイズを設定するか、またはあなたが

findMeButton.frame = CGRectMake(x, y, width, height) 
+0

私はそれを試しましたが、それはまだ動作しません。私は自分の変更を示すために編集しました。 – Chris

+0

CGSizeMake for Swift 3: findMeButton.frame.size = CGSize(幅:50、高さ:50) –

3

スウィフト3

ボタンレイアウトは、以下の操作を実行して設定することができる
findMeButton.frame.size = CGSize(width, height) 
1

...

// Create button with half the size of and centered in parent view 
    parentView.addSubview(button) 
    button.translatesAutoresizingMaskIntoConstraints = false 
    button.widthAnchor.constraint(equalTo: parentView.widthAnchor, multiplier: 0.5).isActive = true 
    button.heightAnchor.constraint(equalTo: parentView.heightAnchor, multiplier: 0.5).isActive = true 
    button.centerXAnchor.constraint(equalTo: parentView.centerXAnchor).isActive = true 
    button.centerYAnchor.constraint(equalTo: parentView.centerYAnchor).isActive = true 
関連する問題