Подтвердить что ты не робот

Как установить высоту и ширину 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 с изображением. Все, что я пробовал, дало мне ошибку или просто не сработало. Я все еще пытаюсь развернуть мою голову вокруг того, что делает translatesAutoresizingMaskIntoConstraints. Я просто хочу иметь кнопку, где она есть, но изменить ее размер (Высота и Ширина).

Заранее спасибо

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: Я полагаю, что для этого нужно использовать Auto Layout, но кто-то может показать мне, как это сделать. Все, что я сделал, не сработало.

4b9b3361

Ответ 1

поэтому здесь я написал код, чтобы добавить кнопку на просмотр.

Свифт 3:

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

Свифт 4:

let button   = UIButton(type: UIButtonType.system) as UIButton
// set the frame
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
// add image
button.setBackgroundImage(UIImage(named:"SearchIcon"), for: .normal)
// button title
button.setTitle("Test Button", for: .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.activate([heightContraints,widthContraints,xContraints,yContraints])

Swift 4.2:

let button   = UIButton(type: UIButton.ButtonType.system) as UIButton
        // set the frame
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
 // add image
button.setBackgroundImage(UIImage(named:"SearchIcon"), for: .normal)
 // button title
button.setTitle("Test Button", for: .normal)
 // add action
button.addTarget(self, action: #selector(didTapOnTakePhotoButton), for: UIControl.Event.touchUpInside)

button.translatesAutoresizingMaskIntoConstraints = false
 // add button on view
self.view.addSubview(button)
 // all constaints
let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 200)
let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([heightContraints,widthContraints,xContraints,yContraints])

Ответ 2

Swift 3

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

Ответ 3

Просто установите размер кнопки с помощью

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

Или вы можете указать расположение кнопки и размер с помощью

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

Ответ 4

Макет кнопки также можно установить, выполнив следующее...

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