activateConstraints:和deactivateConstraints:旋转后没有坚持在IB创建的约束 [英] activateConstraints: and deactivateConstraints: not persisting after rotation for constraints created in IB

查看:1780
本文介绍了activateConstraints:和deactivateConstraints:旋转后没有坚持在IB创建的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 NSLayoutConstraint 方法 activateConstraints: deactivateConstraints:不会出现与IB-创建的约束正常工作(他们正常工作为code-创建的约束)。我创建了一个简单的测试应用程序与有两套制约因素之一按钮。一组,其安装,的centerX和centerY限制,而另一组,其被卸载,具有顶部和左约束(恒定10)。该按钮开关的方法这些约束集。这里是code,

The new NSLayoutConstraint methods activateConstraints: and deactivateConstraints: don't appear to work correctly with IB-created constraints (they do work correctly for code-created constraints). I created a simple test app with one button that has two sets of constraints. One set, which is installed, has centerX and centerY constraints, and the other set, which is uninstalled, has top and left constraints (constant 10). The button method switches these constraints sets. Here is the code,

@interface ViewController ()
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *uninstalledConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *installedConstraints;
@end

@implementation ViewController


- (IBAction)switchconstraints:(UIButton *)sender {
    [NSLayoutConstraint deactivateConstraints:self.installedConstraints];
    [NSLayoutConstraint activateConstraints:self.uninstalledConstraints];
}


-(void)viewWillLayoutSubviews {
    NSLog(@"installed: %@    uninstalled: %@", ((NSLayoutConstraint *)self.installedConstraints[0]).active ? @"Active" : @"Inactive", ((NSLayoutConstraint *)self.uninstalledConstraints[0]).active ? @"Active" : @"Inactive");

}

在应用程序启动,按钮是由它的安装限制定义的正确,中心位置。我做了按钮的操作方法激活/灭活后,该按钮移动到新的位置正确,但是当我旋转视图以景观,它移回到它的最初定义位置(虽然日志仍然显示新激活的设置被激活)。当我回旋转为纵向,按钮停留在其初始位置(在屏幕居中),而现在的日志显示初始设置约束作为活跃的,和那些我激活,为不活动。

When the app launches, the button is in the correct, centered position defined by its installed constraints. After I do the activation/inactivation in the button's action method, the button moves to its new position correctly, but when I rotate the view to landscape, it moves back to its initially defined position (though a log still shows the newly activated set as being active). When I rotate back to portrait, the button stays in its initial position (centered in the screen), and now the log shows that initial set of constraints as active, and the ones I activated, as inactive.

现在的问题是,这是一个错误,或者是这些方法不应该以这种方式与IB定义的约束工作?

The question is, is this a bug, or are these methods not supposed to work in this way with IB defined constraints?

推荐答案

的问题是,你正在做的事情不相干的故事板卸载的约束。他们是有,但不存在。 卸载约束是仅用于大小级!您在使用旋转他们,如果你要令X code交换约束你的自动的。 X code不能与你在做什么应对。但是,如果你创建第二个集code约束,一切都将正常工作。

The problem is that you are doing something incoherent with "uninstalled" constraints in the storyboard. They are there but not there. "Uninstalled" constraints are for use only with size classes! You use them if you are going to let Xcode swap constraints for you automatically on rotation. Xcode can't cope with what you're doing. But if you create the second set of constraints in code, everything will work fine.

那么,做到这一点。删除这两个卸载的限制,并删除 uninstalledConstraints 插座。现在有了这个替换整个视图控制器code:

So, do this. Delete the two "uninstalled" constraints, and delete the uninstalledConstraints outlet. Now replace your entire view controller code with this:

@property (strong, nonatomic) NSMutableArray *c1;
@property (strong, nonatomic) NSMutableArray *c2;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *installedConstraints;
@property (weak,nonatomic) IBOutlet UIButton *button;
@end

@implementation ViewController {
    BOOL did;
}

- (void)viewDidLayoutSubviews {
    NSLog(@"did");
    if (!did) {
        did = YES;
        self.c1 = [self.installedConstraints mutableCopy];
        self.c2 = [NSMutableArray new];
        [self.c2 addObject:
         [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1 constant:30]];
        [self.c2 addObject:
         [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeadingMargin multiplier:1 constant:30]];
    }
}

- (IBAction)switchconstraints:(UIButton *)sender {
    [NSLayoutConstraint deactivateConstraints:self.c1];
    [NSLayoutConstraint activateConstraints:self.c2];
    NSMutableArray* temp = self.c1;
    self.c1 = self.c2;
    self.c2 = temp;
}

现在反复preSS的按钮。正如你看到的,它跳两个位置之间。现在旋转的应用;按钮停留在哪里。

Now repeatedly press the button. As you see, it jumps between the two positions. Now rotate the app; the button stays where it is.

这篇关于activateConstraints:和deactivateConstraints:旋转后没有坚持在IB创建的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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