Change frame programmatically with auto layout

前端 未结 4 1845
春和景丽
春和景丽 2020-12-02 14:23

I have a UITableView with Auto Layout and I need to reduce the height when the GADBannerView appears at the bottom of the screen.

Unfortunat

相关标签:
4条回答
  • 2020-12-02 14:50

    Rather than trying to change the frame of the view, add a height constraint using auto layout and reduce the value of this constraint. Do the below steps:

    1. create a height constarint for your view in your interface.
    2. Then add an IBOutlet object in your class for this constraint. for example,

       @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
      

      Connect this object in your connection panel.

    3. Then change the value of this constraint whenever you needed

       self.heightConstraint.constant = 40;
      
    0 讨论(0)
  • 2020-12-02 15:00

    Let your UITableView constraints to bottom layout is set to 0, make an IBOutlet. Now let your GADBannerView height is 40 so change your outlet.constant = 40; For more about how to make IBOutlet and change its value have a look into this or this hope this will help.

    Edit: For those who seeking for example, follow these simple steps (Because this is accepted answer, I think it is worth to have an example. Credit to @manujmv for this example)

    1. Create a height constraint for your view in your interface.

    2. Then add an IBOutlet object in your class for this constraint. For example:

      @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

    Connect this object in your connection panel.

    1. Then change the value of this constraint whenever you needed

      self.heightConstraint.constant = 40;

    0 讨论(0)
  • 2020-12-02 15:05

    One more thing, you have to call [self.view layoutIfNeeded]; method once you changne the constraints.

    Enjoy :)

    0 讨论(0)
  • 2020-12-02 15:06

    You can find out constraint like that

    extension UIView {
        var heightConstaint: NSLayoutConstraint? {
            get {
                for constraint: NSLayoutConstraint in constraints {
                    if constraint.firstAttribute == .height {
                        if constraint.relation == .equal {
                            return constraint
                        }
                    }
                }
                return nil
            }
    
            set{
                setNeedsLayout()
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题