-1

How can I update this constraint code for iPhone X and up? This code doesn't support the new sizes of views and I feel as if it could be altered just a bit to fit the new specifications. Should an update be made in the function that holds addConstraint ?

@implementation UIView (JSQMessages)


- (void)jsq_pinSubview:(UIView *)subview toEdge:(NSLayoutAttribute)attribute
{


    [self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                     attribute:attribute
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:subview
                                                     attribute:attribute
                                                    multiplier:1.0f
                                                      constant:0.0f]];

}

- (void)jsq_pinAllEdgesOfSubview:(UIView *)subview
{


    [self jsq_pinSubview:subview toEdge:NSLayoutAttributeBottom];
    [self jsq_pinSubview:subview toEdge:NSLayoutAttributeTop];
    [self jsq_pinSubview:subview toEdge:NSLayoutAttributeLeading];
    [self jsq_pinSubview:subview toEdge:NSLayoutAttributeTrailing];
}

@end
Joshua
  • 481
  • 6
  • 21
  • Can you explain what the problem is? The phrase "the new sizes of views" or "new specifications" is meaningless. The code does exactly what it says it will do: it pins a subview exactly to the superview (`self`). It works exactly the way it always has; there is nothing to "update". If you want to do something else — and I'm betting that you do, though you have not explained — then do something else. – matt Jun 15 '20 at 14:53
  • I would also add that no one uses the NSLayoutConstraint initializer any more. Use _anchor_ notation. If you want even more shortcutting, you can use a library such as SnapKit (though personally I am not in favor of interposing an extra dependency for something so crucial). – matt Jun 15 '20 at 14:54

3 Answers3

1

This is code I use for something similar. Adjust to taste.

+ ( void ) embed:( UIView * ) child
        into:( UIView * ) parent
{
    [parent addSubview:child];

    [child.topAnchor    constraintEqualToAnchor:parent.topAnchor].active    = YES;
    [child.rightAnchor  constraintEqualToAnchor:parent.rightAnchor].active  = YES;
    [child.leftAnchor   constraintEqualToAnchor:parent.leftAnchor].active   = YES;
    [child.bottomAnchor constraintEqualToAnchor:parent.bottomAnchor].active = YES;
}

skaak
  • 2,988
  • 1
  • 8
  • 16
  • This is good because it uses anchor notation, as I suggested. But note that it still has nothing to do with the safe area, as implied by the title of the question. We really need more clarification from the OP before an answer is possible. – matt Jun 15 '20 at 15:46
1

Tip 1: start using modern syntax...

Tip 2: don't use "constraint helpers" unless it would really improve your code and workflow.

Tip 3: here's a way to conform to safe area:

- (void)jsq_pinAllEdgesOfSubview:(UIView *)subview
{
    UILayoutGuide *g = [self safeAreaLayoutGuide];
    [NSLayoutConstraint activateConstraints:@[
        [subview.topAnchor constraintEqualToAnchor:g.topAnchor],
        [subview.leadingAnchor constraintEqualToAnchor:g.leadingAnchor],
        [subview.bottomAnchor constraintEqualToAnchor:g.bottomAnchor],
        [subview.trailingAnchor constraintEqualToAnchor:g.trailingAnchor],
    ]];
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
0

Thank you all for the answers, I ended up creating an extension for the delegate and handled constraints that way. This will be helpful in the future for anyone using the deprecated JSQMessages package! Thanks again for the help.

extension JSQMessagesInputToolbar {
override open func didMoveToWindow() {
    super.didMoveToWindow()
    if #available(iOS 11.0, *), let window = self.window {
        let anchor = window.safeAreaLayoutGuide.bottomAnchor
        bottomAnchor.constraint(lessThanOrEqualToSystemSpacingBelow: anchor, multiplier: 0.3).isActive = true
        }
    }
}
Joshua
  • 481
  • 6
  • 21