【问题标题】:How to: Constrain subview to safeArea Obj-c如何:将子视图约束到安全区域对象
【发布时间】:2020-06-15 14:45:49
【问题描述】:

如何为 iPhone X 及更高版本更新此约束代码?这段代码不支持新的视图大小,我觉得它可以稍微改变一下以适应新的规范。是否应该在保存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

【问题讨论】:

  • 你能解释一下问题是什么吗?短语“新的视图大小”或“新规范”是没有意义的。代码完全按照它所说的去做:它将子视图完全固定到父视图 (self)。它的工作方式与以往完全相同;没有什么可以“更新”的。如果你想做其他事情——我敢打赌你会这样做,尽管你没有解释——然后做其他事情。
  • 我还要补充一点,没有人再使用 NSLayoutConstraint 初始化程序了。使用 anchor 表示法。如果您想要更多快捷方式,可以使用 SnapKit 之类的库(尽管我个人不赞成为如此重要的东西插入额外的依赖项)。

标签: objective-c constraints jsqmessagesviewcontroller


【解决方案1】:

这是我用于类似事情的代码。调整口味。

+ ( 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;
}

【讨论】:

  • 这很好,因为它使用锚符号,正如我建议的那样。但请注意,正如问题标题所暗示的那样,它仍然与安全区域无关。在可能得到答案之前,我们确实需要 OP 的更多澄清。
【解决方案2】:

提示 1:开始使用现代语法...

提示 2:不要使用“约束助手”,除非它真的可以改善您的代码和工作流程。

提示 3:这是一种符合安全区域的方法:

- (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],
    ]];
}

【讨论】:

    【解决方案3】:

    感谢大家的回答,我最终为委托创建了一个扩展并以这种方式处理约束。这对于使用已弃用的 JSQMessages 包的任何人将来都会有所帮助!再次感谢您的帮助。

    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多