Make a UIView stick to bottom of the ViewController frame in Swift

17,246

Solution 1

hmmm try this...

override func viewDidLoad() {
  super.viewDidLoad()

  let theHeight = view.frame.size.height //grabs the height of your view

  var nextBar = UIView()

  nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)

  nextBar.frame = CGRect(x: 0, y: theHeight - 50 , width: self.view.frame.width, height: 50)

  self.view.addSubview(nextBar)

}

for the 'y:' in CRect, you're taking the size, in height, of the screen size and you're subtracting how many ever pixels you want. It will appear the same for what ever screen size.

Solution 2

You can do it programmatically using constraints

fileprivate func setupName(){
        let height = CGFloat(50)

        lblName.text = "Hello world"
        lblName.backgroundColor = .lightGray

        //Step 1
        lblName.translatesAutoresizingMaskIntoConstraints = false

        //Step 2
        self.view.addSubview(lblName)

        //Step 3
        NSLayoutConstraint.activate([

            lblName.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
            lblName.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
            lblName.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -height),
            lblName.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
            ])

    }

Please refer link for more details

enter image description here

Share:
17,246
Nick89
Author by

Nick89

Great passion for digital technology and innovation. I have some experience in HTML and CSS. Learning to code Swift with no prior experience in mobile app coding. Hobbies include Snowboarding, football, and running my small business in Photography and Filming.

Updated on June 07, 2022

Comments

  • Nick89
    Nick89 almost 2 years

    I have created a UIView (it's just a blue rectangle) and I want it to stick to the bottom of the frame/view of the screen.

    var nextBar = UIView()
    
    nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
    nextBar.frame = CGRect(x: 0, y: 600, width: self.view.frame.width, height: 50)
    self.view.addSubview(nextBar)
    

    I'm assuming i need to add something in after the 'y:' part in the CGRect, but i can't seem to make it work or find anywhere that shows me this in swift.

    Thanks in advance

  • Nick89
    Nick89 almost 9 years
    Thanks, can you explain why and what the * 0.95 is doing?
  • whereisleo
    whereisleo almost 9 years
    yeah. So when you create the 'let theHeight..' you are taking the Float of the screen size. Whether it's a 4s, 5s, 6, or 6+. So that number is maximum size of the screen. the '0.95' is a decimal or saying 95%. It's like saying, there a room thats 100ft long and someone tells you to point where 95% to the end of the room would be and u would roughly guess correctly, it doesnt matter if the room is 20ft, 50ft or 100ft long...it's just a percentage (%)...did it work?
  • Nick89
    Nick89 almost 9 years
    Thanks for that. It did place the view at the bottom of the frame (i set it to 0.915), but it's different depending on what screen size it's on. So it appears fine on a 5s, but on a iPhone 6 its out of place again.
  • whereisleo
    whereisleo almost 9 years
    or: '...y: theHeight - 20...' . your taking the screen size minus how many ever pixels u want. let me know if it works cause i need some points!! lol
  • Nick89
    Nick89 almost 9 years
    that worked!! I set it to the height of what i made the View to be (in this case 50) so: theHeight - 50 , Thanks for the help, if you amend the answer you did above i shall accept it for you!
  • whereisleo
    whereisleo almost 9 years
    Ok, I just up dated my answer! :)
  • znelson
    znelson about 4 years
    You're a lifesaver, 5 years later!