2016-05-27 13 views
7

Questo mi sta facendo impazzire. Ho cercato ovunque e non riesco a capirlo. Dai un'occhiata ...Come si imposta l'altezza e la larghezza di un UIButton con l'immagine

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 

Sto ancora imparando iOS. Come si imposta l'altezza e la larghezza di questo UIButton con un'immagine. Tutto ciò che ho provato mi ha dato un errore o semplicemente non ha funzionato. Sto ancora provando a stupirmi di ciò che traduce l'auto-dimensionamento della maschera IntoConstraints. Voglio semplicemente avere il pulsante dove è, ma cambiare le sue dimensioni (altezza & larghezza).

Grazie in anticipo

EDIT: ho cambiato il pezzo di codice a questo

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

voglio posizionare il pulsante a fondo Windows e margine destro. Voglio anche impostare le dimensioni dell'immagine in altezza 50 x larghezza 50. Come potrei realizzare questo?

MODIFICA 2: Immagino che si debba usare Auto Layout per farlo, ma qualcuno può mostrarmi come. Tutto ciò che ho fatto non ha funzionato.

+0

set di immagini di sfondo in è iniziare a lavorare bene –

+0

Cosa fare vuoi dire con quello? – Chris

+0

devi impostare quell'immagine come immagine di sfondo del pulsante. –

risposta

8

ecco ho scritto un codice per aggiungere il pulsante a vista.

 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

Sì, lo so come fare ma vorrei utilizzare il layout automatico per ancorarlo ai margini inferiore e destro, ma quando Lo faccio, non mi permette di impostare la dimensione dell'immagine. – Chris

+0

ok, quindi è necessario aggiungere layout automatico per fondo, centro orizzontale, larghezza e altezza anche –

+0

Potrebbe mostrarmi il codice. Ho anche provato questo e mi dà errori. E scusa, sono un principiante in questo. – Chris

4

semplicemente impostare le dimensioni del pulsante con

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

oppure è possibile specificare la posizione del pulsante e la dimensione con

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

L'ho appena provato e ancora non funziona. Ho modificato per mostrare i miei cambiamenti. – Chris

+0

CGSizeMake per Swift 3: findMeButton.frame.size = CGSize (larghezza: 50, altezza: 50) –

3

Swift immagine 3

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

La disposizione dei tasti può essere impostato anche nel modo seguente ...

// 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 
Problemi correlati