Search code examples
iosswiftxcodeautolayoutios-autolayout

Positioning a button on bottom right of screen


I'm having trouble with the positioning of my button. I am trying to position my button on the bottom right of my screen. I am new with auto layouts. The button appears currently on the top left of the screen.

Here is my code:

  1. add markers
  2. add the map view
  3. add the button

heres where I add markers:

func secondfunction() {

        for x in names{
            let url1 = URL(string: url: ", url1)
            let data1 = try? Data(contentsOf: url1!) //make sure your image in this url does exist
            //self.imagesOne = UIImage(data: data1!)
            self.images.append(UIImage(data: data1!)!)
        }

        self.loadFunction()
    }

heres where I load map and add button:

  func loadFunction()
        {

            mapView = MGLMapView(frame: view.bounds)
            mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            mapView.centerCoordinate = CLLocationCoordinate2D(latitude:xCoord, longitude: yCoord)

            mapView.zoomLevel = 15
            mapView.delegate = self
            view.addSubview(mapView)

            var pointAnnotations = [MGLPointAnnotation]()
            for coordinate in locationsList {
                let location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
                let point = MGLPointAnnotation()
                point.title = "Tap here"
                point.coordinate = location
                pointAnnotations.append(point)
            }

            self.tabBarController?.tabBar.isHidden = false

            mapView.addAnnotations(pointAnnotations)

            button.setBackgroundImage(UIImage(named:"compass.png"), for: .normal)
            button.addTarget(self, action: #selector(btnPressed), for: UIControl.Event.touchUpInside)

            self.view.addSubview(button)


            button.translatesAutoresizingMaskIntoConstraints = false
            let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)

            let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)

            let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.bottomMargin, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.bottomMargin, multiplier: 1, constant: -80)

            let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: -80)

            NSLayoutConstraint.activate([heightContraints,widthContraints,xContraints,yContraints])

        }

Solution

  • Give your button constraint to bottom like this .. this is working tested code

    override func viewDidLoad() {
        super.viewDidLoad()
    
               button.backgroundColor = .red
               self.view.addSubview(button)
    }
    override func viewDidLayoutSubviews() {
        
        
        button.translatesAutoresizingMaskIntoConstraints = false
        let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)
        
        let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)
        
        let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.bottomMargin, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.bottomMargin, multiplier: 1, constant: -20)
        
        let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: -20)
        
        NSLayoutConstraint.activate([heightContraints,widthContraints,xContraints,yContraints])
    }
    

    if you want to position it right give it constraint from right margin

    change constant according to your design preference

    enter image description here