Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UILabel作为子视图添加到UITextView时存在不明确的约束_Ios_Swift3 - Fatal编程技术网

Ios 将UILabel作为子视图添加到UITextView时存在不明确的约束

Ios 将UILabel作为子视图添加到UITextView时存在不明确的约束,ios,swift3,Ios,Swift3,问题 我将UITextView子类化,并将UILabel添加为子视图。我想把标签放在右下角 代码 fileprivate lazy var counterLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false self.addSubview(label) // add constraints self.addCo

问题
我将
UITextView
子类化,并将
UILabel
添加为子视图。我想把标签放在右下角

代码

  fileprivate lazy var counterLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(label)

    // add constraints
    self.addConstraints([
      NSLayoutConstraint(item: label, equalTo: self, attribute: .right),
      NSLayoutConstraint(item: label, equalTo: self, attribute: .bottom)]
    )

    return label
  }()
屏幕截图

如上面的屏幕截图所示,红色标签的布局不明确


如果我使用故事板,并添加相同的两个约束(底部,右侧),则标签的位置不会模棱两可。我做错了什么?

您想要的是superview。将标签和文本视图作为子视图添加到此superview。

问题可能是由于instrict约束。由于内部的文本,所有标签都有自己的展开约束。这种限制具有一定的优先性。可能的问题是您创建的约束优先级较低,这导致了此类问题。尝试使用完整功能创建约束。这对我来说很好。这是我的代码和文本字段

self.leadingLabelConstraint = [NSLayoutConstraint constraintWithItem:_markLabel
                                                               attribute:NSLayoutAttributeLeading
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self
                                                               attribute:NSLayoutAttributeLeading
                                                              multiplier:1.0 constant:0];
    self.topLabelConstraint = [NSLayoutConstraint constraintWithItem:_markLabel
                                                           attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:self
                                                           attribute:NSLayoutAttributeTop
                                                          multiplier:1.0 constant:0];
    [self.superview addConstraints:@[self.topLabelConstraint, self.leadingLabelConstraint]];
    self.heightLabelConstraint = [NSLayoutConstraint constraintWithItem:_markLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:20];
    [self.markLabel addConstraint:self.heightLabelConstraint];
self是-UITextField


而markLabel是-UILabel。

标签怎么可能是文本视图的子视图?文本视图是可滚动的;它会把标签滚动到看不见的地方。啊,我太傻了!谢谢你的帮助!