ios 使用自动布局以编程方式更改框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23655096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:08:13  来源:igfitidea点击:

Change frame programmatically with auto layout

iosobjective-cuitableviewautolayout

提问by Fry

I've a UITableViewwith Autolayout and I need to reduce the height when the GADBannerViewappears at the bottom of the screen.

我有一个自动UITableView布局,当GADBannerView出现在屏幕底部时,我需要降低高度。

Unfortunately with Autolayout is impossible modify the frame. The solution is remove Autolayout and set the frame manually. This is very dangerous because all my apps works fine on 3.5' and 4.0' display and removing auto layout causing a new testing phase and more effort.

不幸的是,自动布局是不可能修改框架的。解决方案是删除自动布局并手动设置框架。这是非常危险的,因为我所有的应用程序在 3.5' 和 4.0' 显示器上都可以正常工作,并且删除自动布局会导致新的测试阶段和更多的工作。

Is there a way to change frame even if auto layout is enabled ?

即使启用了自动布局,有没有办法更改框架?

回答by Bharat

Let your UITableViewconstraints to bottom layout is set to 0, make an IBOutlet. Now let your GADBannerViewheight is 40 so change your outlet.constant = 40;For more about how to make IBOutletand change its value have a look into thisor thishope this will help.

让您UITableView对底部布局的约束设置为 0,制作一个IBOutlet. 现在让你的GADBannerView身高是 40 所以改变你的outlet.constant = 40;关于如何制作IBOutlet和改变它的价值的更多信息,看看这个这个希望这会有所帮助。

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)

编辑:对于那些寻求例如的人,请按照以下简单步骤操作(因为这是公认的答案,我认为值得举个例子。这个例子归功于@manujmv)

  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;

  1. 在您的界面中为您的视图创建高度约束。

  2. 然后在您的类中为此约束添加一个 IBOutlet 对象。例如:

    @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;

  1. 然后在需要时更改此约束的值

    self.heightConstraint.constant = 40;

回答by manujmv

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;
    
  1. 在您的界面中为您的视图创建一个高度约束。
  2. 然后在您的类中为此约束添加一个 IBOutlet 对象。例如,

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

    在您的连接面板中连接此对象。

  3. 然后在需要时更改此约束的值

     self.heightConstraint.constant = 40;
    

回答by RJ168

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

还有一件事,[self.view layoutIfNeeded];一旦你改变了约束,你就必须调用方法。

Enjoy :)

享受 :)

回答by AshvinGudaliya

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()
        }
    }
}