2016-05-27 8 views
7

Dies macht mich verrückt. Ich habe überall gesucht und kann das nicht herausfinden. Schau es dir an ...Wie stellen Sie die Höhe und Breite eines UIButton mit Bild

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 

Ich lerne immer noch iOS. Wie legen Sie die Höhe und die Breite dieses UIButton mit einem Bild fest. Alles, was ich versucht habe, hat mir einen Fehler oder einfach nicht funktioniert. Ich versuche immer noch, mir den Kopf zu zerreißen, was TranslatorsizingMaskIntoConstraints tut. Ich möchte einfach nur den Button wo er ist aber seine Größe ändern (Höhe & Breite).

Vielen Dank im Voraus

EDIT: Ich habe das Stück Code zu diesem

// 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) 

Ich möchte

verändert die Schaltfläche, um die Fenster unteren und rechten Rand zu positionieren. Ich möchte auch die Bildmaße auf Höhe 50 x Breite 50 einstellen. Wie würde ich das erreichen?

EDIT 2: Ich denke, Sie müssen Auto Layout verwenden, um dies zu tun, aber kann mir jemand wie zeigen. Alles, was ich getan habe, hat nicht funktioniert.

+0

Set Bild starten feine –

+0

arbeiten Was tun meinst du damit? – Chris

+0

müssen Sie dieses Bild als Schaltflächenhintergrundbild festlegen. –

Antwort

8

so hier schrieb ich einen Code auf die Schaltfläche Standort und die Größe festlegen, die Taste auf Ansicht hinzuzufügen.

 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

Ja, ich weiß, wie das geht, aber ich möchte Auto Layout verwenden, um es an den unteren und rechten Rändern zu verankern, aber wann Ich mache das, damit ich die Bildgröße nicht einstellen kann. – Chris

+0

ok, als Sie Auto-Layout für unten, Horizontale Mitte und Breite und Höhe hinzufügen müssen –

+0

Könnten Sie mir im Code zeigen. Ich habe das auch ausprobiert und es gibt mir Fehler. Und entschuldige, dass ich ein Neuling bin. – Chris

4

Stellen Sie einfach auf den Button Größe mit

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

Oder Sie können mit

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

Ich habe es gerade versucht und es funktioniert immer noch nicht. Ich habe bearbeitet, um meine Änderungen zu zeigen. – Chris

+0

CGSizeMake for Swift 3: findMeButton.frame.size = CGSize (Breite: 50, Höhe: 50) –

3

Swift 3

findMeButton.frame.size = CGSize(width, height) 
1

Das Tasten-Layout auch, indem Sie folgende ...

eingestellt werden kann, im Hintergrund Bild
// 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 
Verwandte Themen