0

I have a view with a number of subviews. The subviews all have size constraints, built in the storyboard.

part of viewDidLoad() is to programmatically add a number of buttons. And I'm using the subview's bounds to known where to add the buttons to take up available screen real estate. As a simplified example:

override func viewDidLoad()
{
    super.viewDidLoad()

    let bottom = buttonView.bounds.maxY
    let right = buttonView.bounds.maxX

    let button = UIButton()
    button.frame = CGRect(x: CGFloat(0.0),
                          y: CGFloat(0.0),
                          width: CGFloat(right),
                          height: CGFloat(bottom))
    button.addTarget(self, action: #selector(buttonPushed(_:)), for: .touchUpInside)
    buttonView.addSubview(button)

    print("buttonview frame = \(buttonView.frame)")
}

override func viewWillAppear()
{
    super.viewWillAppear()

    print("buttonview frame = \(buttonView.frame)")
}

What I see happen in the simulator (and on hardware) is that the views are all sized according to how I had it designed in the storyboard, matching the hardware I had designed it on (iPhone 11). When I run it on an iPhone 8, my subviews all run off screen, and the first print statement indicates a width of 414.0. The constraints are applied sometime before viewWillAppear, and the second print statement indicates a width of 375.0. But my button isn't resized to fit. It still continues off screen.

How do I programmatically add my button subviews (this example only has the 1, code has 20 being built in a loop) to adhere to the constraints of the superview bounds, after I know what they actually are?

wrjohns
  • 484
  • 4
  • 14
  • whats buttonView? you are getting the entire height of the container and using it as the height of the button? – Joshua Mar 05 '20 at 03:08
  • Sorry, buttonView is an instance of UIView. And I’m creating anywhere from 4 to 20 buttons depending on other options. To make best use of screen real estate, I’m creating the buttons programmatically with sizes and positions from a table lookup based on other options. For purposes of a small test case, I kept it simple with a single button that takes the entire bounds of its UIView. – wrjohns Mar 05 '20 at 04:52

1 Answers1

0

you have to add constraint by code, for example.

1) first I will create the button.

    lazy var closeButton : UIButton = {
        let button = UIButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = .blue
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        return button
    }()

2) with this code I will add the button to the main view, and set the constraint.

self.view.addSubview(closeButton)
NSLayoutConstraint.activate([
            closeButton.heightAnchor.constraint(equalToConstant: 20),
            closeButton.widthAnchor.constraint(equalToConstant: 20),
            closeButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20),
            closeButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)]

but if you want to understand really how work this please the follow link How to add constraints programmatically using Swift

cristian_064
  • 576
  • 3
  • 16