Вопрос

I have the following Xib file form my project: XIB file

I tried to add to hint view some new constraints (bottom, top, left and right space to super view), but it's impossible from interface builder as image shows. How it should be done?

Corresponding function:

+ (instancetype)presentInViewController:(UIViewController *)viewController withDefaultsKey:(NSString *)defaultsKey {
    ELHintViewOwner *owner = [ELHintViewOwner new];
    NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
    //hintViewOwner.decoupledView.delegateViewController = viewController
    ELHintView *hintView = [bundle firstObject];
    hintView.frame = viewController.view.bounds;
    hintView.titleLabel.text = @"";
    hintView.defaultsKey = defaultsKey;
    hintView.tapAnywhereLabel.text = NSLocalizedString(@"Tap anywhere to continue", nil);
    hintView.showLabel.text = NSLocalizedString(@"Don't show this message again", nil);
    hintView.imageView.hidden = YES;
    hintView.showSwitch.on = ![[[NSUserDefaults standardUserDefaults] valueForKey:defaultsKey]  boolValue];

    if ([hintView shouldShow])
        [viewController.view addSubview:owner.decoupledView];
    return hintView;
}
Это было полезно?

Решение

You will have to add the autolayout constraints in the code if you are adding the superview to some other view.

+ (instancetype)presentInViewController:(UIViewController *)viewController withDefaultsKey:(NSString *)defaultsKey {
    ELHintViewOwner *owner = [ELHintViewOwner new];
    NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
    //hintViewOwner.decoupledView.delegateViewController = viewController
    ELHintView *hintView = [bundle firstObject];
    hintView.frame = viewController.view.bounds;
    hintView.titleLabel.text = @"";
    hintView.defaultsKey = defaultsKey;
    hintView.tapAnywhereLabel.text = NSLocalizedString(@"Tap anywhere to continue", nil);
    hintView.showLabel.text = NSLocalizedString(@"Don't show this message again", nil);
    hintView.imageView.hidden = YES;
    hintView.showSwitch.on = ![[[NSUserDefaults standardUserDefaults] valueForKey:defaultsKey]  boolValue];

    if ([hintView shouldShow]){
        [viewController.view addSubview:owner.decoupledView];
          hintView.translatesAutoresizingMaskIntoConstraints = NO;
          NSArray *constraintsX = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[hintView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(hintView)];
          NSArray *constraintsY = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[hintView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(hintView)];
          [viewController.view addConstraints:constraintsX];
          [viewController.view addConstraints:constraintsY];
     }
    return hintView;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top