<small id='cwmcB'></small><noframes id='cwmcB'>

    <legend id='cwmcB'><style id='cwmcB'><dir id='cwmcB'><q id='cwmcB'></q></dir></style></legend>

  1. <tfoot id='cwmcB'></tfoot>
    <i id='cwmcB'><tr id='cwmcB'><dt id='cwmcB'><q id='cwmcB'><span id='cwmcB'><b id='cwmcB'><form id='cwmcB'><ins id='cwmcB'></ins><ul id='cwmcB'></ul><sub id='cwmcB'></sub></form><legend id='cwmcB'></legend><bdo id='cwmcB'><pre id='cwmcB'><center id='cwmcB'></center></pre></bdo></b><th id='cwmcB'></th></span></q></dt></tr></i><div id='cwmcB'><tfoot id='cwmcB'></tfoot><dl id='cwmcB'><fieldset id='cwmcB'></fieldset></dl></div>

        <bdo id='cwmcB'></bdo><ul id='cwmcB'></ul>
    1. 移动带有约束的视图

      时间:2023-09-11
      <i id='Kl5WM'><tr id='Kl5WM'><dt id='Kl5WM'><q id='Kl5WM'><span id='Kl5WM'><b id='Kl5WM'><form id='Kl5WM'><ins id='Kl5WM'></ins><ul id='Kl5WM'></ul><sub id='Kl5WM'></sub></form><legend id='Kl5WM'></legend><bdo id='Kl5WM'><pre id='Kl5WM'><center id='Kl5WM'></center></pre></bdo></b><th id='Kl5WM'></th></span></q></dt></tr></i><div id='Kl5WM'><tfoot id='Kl5WM'></tfoot><dl id='Kl5WM'><fieldset id='Kl5WM'></fieldset></dl></div>
      • <bdo id='Kl5WM'></bdo><ul id='Kl5WM'></ul>

        <small id='Kl5WM'></small><noframes id='Kl5WM'>

        <tfoot id='Kl5WM'></tfoot>

      • <legend id='Kl5WM'><style id='Kl5WM'><dir id='Kl5WM'><q id='Kl5WM'></q></dir></style></legend>

                  <tbody id='Kl5WM'></tbody>
                本文介绍了移动带有约束的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的视图控制器中有几个视图在检测到向上滑动时向上移动,然后在检测到向下滑动时向下移动.我通过使用 CGRectOffset 调整 y 原点来强制视图移动.我现在已经使用 IB 对我的视图应用了约束,但我不确定移动视图的最佳方式是什么,以便它们最终在 iphone 5、6 和 6+ 上处于正确的位置.

                I have a couple views in my view controller that move up when an up swipe is detected then down when a down swipe is detected. I was forcing the views to move by adjusting the y origin using CGRectOffset. I've now applied constraints to my views with IB and I'm not sure whats the best way to move the views so that they end up in the right position on the iphone 5, 6, and 6+.

                目前我正在做这样的事情:

                Currently I'm doing something like this:

                [self.view layoutIfNeeded];
                
                    self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338;
                
                    [UIView animateWithDuration:5
                                     animations:^{
                                         [self.view layoutIfNeeded];
                                     }];
                

                使用比率更改常数会更好吗?那么对于上面的约束,与其使用 338,不如这样做:

                Is it better to change the constants using ratios? So for the constraint above, instead of using 338, would it be better to do this:

                    self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant + (self.panView.frame.size.height/1.680);
                
                    //self.panView.frame.size.height = 568
                    //(568/1.680) = 338
                

                推荐答案

                是的,更改常量时没有问题.这里的问题是你必须适当地设置你的约束.

                Yes, no issue when you change the constants. The thing here is that you have to set your constraint appropriately.

                我们来看一个例子.

                我在 Storyboard 中有一个 UIView,我想更改它的宽度.它的默认宽度是 1024.

                I have a UIView in Storyboard and I would like to change its width. Its default width is 1024.

                经过一些动画,我们将其宽度改为900.

                After some animation, We will change it's width to 900.

                请按照以下步骤实现:

                1. 选择我们要在其中更新约束的UIView.这里我们需要更新width,所以我们要为width添加一个约束.
                1. Select UIView in which we want to update constraints. Here we need to update width, So we will add a constraint for width.

                1. 现在我们可以看到添加到 UIView 的新约束.
                1. Now we can see new constraint added to UIView.

                1. 单击该约束.它将显示约束属性.现在我们想将宽度从 1024 减小到 900,以便在约束属性中将 Relation 属性更改为 Less Than or Equal.同样,如果您想从 1024 增加宽度,则 Relation大于或等于.
                1. Click on that constraint. It will show constraint properties. Here now we want to decrease width from 1024 to 900, So that in constraint property change Relation property to Less Than or Equal. Similarly, if you like to increase width from 1024 then Relation will be Greater Than or Equal.

                1. 现在为 NSLayoutConstraint 创建一个 IBOutlet 变量,并将其与上述宽度约束连接起来.

                1. Now create an IBOutlet variable for NSLayoutConstraint and connect it with above width constraint.

                改变宽度常数

                Swift 5.0

                @IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!
                
                func reduceWidth() {
                    // Reduce width of view.
                    UIView.animate(withDuration: 0.35, animations: { () -> Void in
                        self.viewWidthConstraint.constant = 900
                        self.view.layoutIfNeeded()
                    })
                }
                
                func normalWidth() {
                    // Change to the default width of view.
                    UIView.animate(withDuration: 0.35, animations: { () -> Void in
                        self.viewWidthConstraint.constant = 1024
                        self.view.layoutIfNeeded()
                    })
                }
                

                目标 C

                @property (weak, nonatomic) IBOutlet NSLayoutConstraint     *viewWidthConstraint; 
                
                - (void) reduceWidth {
                    // Reduce width of view.
                    [UIView animateWithDuration:0.35f animations:^{
                        self.viewWidthConstraint.constant = 900;
                        [self.view layoutIfNeeded];
                    }];
                }
                
                - (void) normalWidth {
                    // Change to default width of view.
                    [UIView animateWithDuration:0.35f animations:^{
                        self.viewWidthConstraint.constant = 1024;
                        [self.view layoutIfNeeded];
                    }];
                }
                

                这篇关于移动带有约束的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用自动布局保持圆形 imageView 圆形? 下一篇:iOS自动布局:两个等宽的按钮,并排

                相关文章

                  <bdo id='Lpdus'></bdo><ul id='Lpdus'></ul>

                  <legend id='Lpdus'><style id='Lpdus'><dir id='Lpdus'><q id='Lpdus'></q></dir></style></legend>
                1. <i id='Lpdus'><tr id='Lpdus'><dt id='Lpdus'><q id='Lpdus'><span id='Lpdus'><b id='Lpdus'><form id='Lpdus'><ins id='Lpdus'></ins><ul id='Lpdus'></ul><sub id='Lpdus'></sub></form><legend id='Lpdus'></legend><bdo id='Lpdus'><pre id='Lpdus'><center id='Lpdus'></center></pre></bdo></b><th id='Lpdus'></th></span></q></dt></tr></i><div id='Lpdus'><tfoot id='Lpdus'></tfoot><dl id='Lpdus'><fieldset id='Lpdus'></fieldset></dl></div>

                2. <small id='Lpdus'></small><noframes id='Lpdus'>

                  <tfoot id='Lpdus'></tfoot>