移动有约束的视图 [英] Moving views with constraints

查看:24
本文介绍了移动有约束的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中有几个视图,它们在检测到向上滑动时向上移动,然后在检测到向下滑动时向下移动.我通过使用 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.

请按照以下步骤实现此目的:

Follow steps below to achieve this:

  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 属性更改为 小于或等于.同样,如果您想从 1024 开始增加宽度,那么关系将是大于或等于.
  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];
    }];
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆