Ios UIView设置约束

Ios UIView设置约束,ios,swift,Ios,Swift,我无法通过编程方式创建UIView实例。我有一个返回视图的函数,这样我就可以在UITextView中的NSAttachment中使用它 这就是我努力实现的目标: 这是我在模拟器中得到的: 下面的代码: let fullView = UIView() let firstButton = UIButton() let secondButton = UIButton() let thirdTextView = UITextView() fullView.frame

我无法通过编程方式创建
UIView
实例。我有一个返回视图的函数,这样我就可以在
UITextView
中的
NSAttachment
中使用它

这就是我努力实现的目标: 这是我在模拟器中得到的:

下面的代码:

let fullView = UIView()
    let firstButton = UIButton()
    let secondButton = UIButton()
    let thirdTextView = UITextView()

    fullView.frame = CGRect(x: 0, y: 0, width: textView.frame.width, height: 90)
    fullView.backgroundColor = UIColor(red:0.82, green:0.83, blue:0.85, alpha:1.0)

    firstButton.setTitle(text1, for: .normal)
    firstButton.setTitleColor(.black, for: .normal)
    firstButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
    firstButton.contentHorizontalAlignment = .left

    secondButton.setTitle("Button2", for: .normal)
    secondButton.setTitleColor(.black, for: .normal)
    secondButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
    secondButton.contentHorizontalAlignment = .right

    thirdTextView.text = text2

    let descriptionBarStackView = UIStackView()
    descriptionBarStackView.axis = .horizontal
    descriptionBarStackView.alignment = .fill 
    descriptionBarStackView.distribution = .fillProportionally 

    descriptionBarStackView.addArrangedSubview(firstButton)
    descriptionBarStackView.addArrangedSubview(secondButton)

    let viewWithStackViews = UIStackView()
    viewWithStackViews.axis = .vertical
    viewWithStackViews.alignment = .fill // .leading .firstBaseline .center .trailing .lastBaseline
    viewWithStackViews.distribution = .fillEqually
    viewWithStackViews.addArrangedSubview(descriptionBarStackView)
    viewWithStackViews.addArrangedSubview(thirdTextView)


    fullView.addSubview(viewWithStackViews)

    descriptionBarStackView.translatesAutoresizingMaskIntoConstraints = false
    thirdTextView.translatesAutoresizingMaskIntoConstraints = false
    viewWithStackViews.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
    viewWithStackViews.topAnchor.constraint(equalTo: fullView.topAnchor, constant: 5),
    viewWithStackViews.leadingAnchor.constraint(equalTo: fullView.leadingAnchor, constant: 5),
    viewWithStackViews.trailingAnchor.constraint(equalTo: fullView.trailingAnchor, constant: 5),
    viewWithStackViews.bottomAnchor.constraint(equalTo: fullView.bottomAnchor, constant: 5),
    ])
编辑:

您正在设置
viewWithStackViews.distribution=.FillView
由于
.fillquality
的原因,
堆栈视图将为子视图分配相同的高度。这就是为什么在
视图的一半中看到按钮,而在剩余的一半中看到
第三个文本视图。
因此,将其更改为:
viewWithStackViews.distribution=.Fill成比例

还有一件事:您的
thirdTextView
并没有根据其内容获得其高度。 尝试此操作以允许您
thirdTextView
自动获取高度。如果文本可能太长,请将
ScrolleEnabled
设置为true,并为textView设置高度约束

thirdTextView.translatesAutoResizezingMaskintoConstraints=true
thirdTextView.sizeToFit()
thirdTextView.scrollEnabled=false

您正在设置
viewWithStackViews.distribution=.FillView
由于
.fillquality
的原因,
堆栈视图将为子视图分配相同的高度。这就是为什么在
视图的一半中看到按钮,而在剩余的一半中看到
第三个文本视图。
因此,将其更改为:
viewWithStackViews.distribution=.Fill成比例

还有一件事:您的
thirdTextView
并没有根据其内容获得其高度。 尝试此操作以允许您
thirdTextView
自动获取高度。如果文本可能太长,请将
ScrolleEnabled
设置为true,并为textView设置高度约束

thirdTextView.translatesAutoResizezingMaskintoConstraints=true
thirdTextView.sizeToFit()
thirdTextView.scrollEnabled=false

检查此更新的方法

}

这里我列出了一些变化

  • mainView.frame.height基于主文本视图的全高
  • firstButton.backgroundColor=.clear
  • secondButton.backgroundColor=.clear
  • thirdTextView.backgroundColor=.clear
  • 按钮边距 descriptionBarStackView.IsLayoutMarginRelativeArrange=true descriptionBarStackView.directionalLayoutMargins=NSDirectionalEdgeInsets(顶部:0,前导:5,底部:0,尾随:5)
  • NSLayoutConstraint在全视图中从零到全部
  • fullView.layer.cornerRadius=5
这就是结果。。

检查此更新的方法

}

这里我列出了一些变化

  • mainView.frame.height基于主文本视图的全高
  • firstButton.backgroundColor=.clear
  • secondButton.backgroundColor=.clear
  • thirdTextView.backgroundColor=.clear
  • 按钮边距 descriptionBarStackView.IsLayoutMarginRelativeArrange=true descriptionBarStackView.directionalLayoutMargins=NSDirectionalEdgeInsets(顶部:0,前导:5,底部:0,尾随:5)
  • NSLayoutConstraint在全视图中从零到全部
  • fullView.layer.cornerRadius=5
这就是结果。。


试图保留您的布局结构


试图保留您的布局结构


您需要设置trailingAnchor常量-5而不是5。您需要设置trailingAnchor常量-5而不是5。谢谢!3个问题。1.我希望按钮的高度不要增长,所以它将始终是固定的,而不是创造空间。2.我需要视图高度根据thirdTextView.text 3中提供的文本数量增长。我需要将宽度设置为textView容器的宽度似乎mainView.frame.width使其太宽了…。@a.wip对于您的第一个que答案是descriptionBarStackView.TranslatesAutoResistingGMaskintoConstraints=false NSLayoutConstraints(项:descriptionBarStackView,属性:NSLayoutConstraint.attribute.height,relatedBy:NSLayoutConstraint.Relation.equal,toItem:nil,属性:NSLayoutConstraint.attribute.NotaAttribute,乘数:1,常数:30).isActive=true viewWithStackViews.distribution=.fill第二个que集的高度取决于文本,第三个que集的宽度取决于文本,从textview作为u SETOK,将它们添加到我的代码中。我的宽度仍然有问题,我在上面添加了一个编辑。它似乎仍在被切断。好的,让u将视图的边距和约束设置为零WithStackViews?我正在使用注释和函数中的代码。我缺少了什么?谢谢!3个问题。1.我希望按钮高度不增加,因此它将始终是固定的,不会产生空间。2.我需要视图高度增加,这取决于thirdTextView中提供了多少文本。文本3.我需要宽度为的宽度textView容器it been mainView.frame.width使其太宽了…。@a.wip对于您的第一个que答案是descriptionBarStackView.TranslatesAutoResistingGMaskintoConstraints=false NSLayoutConstraint(项:descriptionBarStackView,属性:NSLayoutConstraint.attribute.height,relatedBy:NSLayoutConstraint.Relation.equal,toItem:nil,属性:NSLayoutConstraint.attribute.NotaAttribute,乘数:1,常数:30).isActive=true viewWithStackViews.distribution=.fill第二个que集的高度取决于文本,第三个que集的宽度取决于文本,从textview作为u SETOK,将它们添加到我的代码中。我的宽度仍然有问题,我在上面添加了一个编辑。它似乎仍在被切断。好的,让u将视图的边距和约束设置为零WithStackViews?我正在使用注释和函数中的代码。我缺少什么?我如何使视图变得更大,取决于thirdTextView中有多少文本?签出这个对我不起作用的代码…我可以做些什么吗
func customView(){

let fullView = UIView()
let firstButton = UIButton()
let secondButton = UIButton()
let thirdTextView = UITextView()

fullView.frame = CGRect(x: 0, y: 0, width: mainView.frame.width, height: mainView.frame.height)
fullView.backgroundColor = UIColor(red:0.82, green:0.83, blue:0.85, alpha:1.0)

firstButton.setTitle("Button1", for: .normal)
firstButton.setTitleColor(.black, for: .normal)
firstButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
firstButton.contentHorizontalAlignment = .left
firstButton.backgroundColor = .clear

secondButton.setTitle("Button2", for: .normal)
secondButton.setTitleColor(.black, for: .normal)
secondButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
secondButton.contentHorizontalAlignment = .right
secondButton.backgroundColor = .clear

thirdTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
thirdTextView.backgroundColor = .clear

let descriptionBarStackView = UIStackView()
descriptionBarStackView.axis = .horizontal
descriptionBarStackView.alignment = .fill
descriptionBarStackView.distribution = .fillProportionally
descriptionBarStackView.isLayoutMarginsRelativeArrangement = true
descriptionBarStackView.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5)

descriptionBarStackView.addArrangedSubview(firstButton)
descriptionBarStackView.addArrangedSubview(secondButton)

let viewWithStackViews = UIStackView()
viewWithStackViews.axis = .vertical
viewWithStackViews.alignment = .fill // .leading .firstBaseline .center .trailing .lastBaseline
viewWithStackViews.distribution = .fill
viewWithStackViews.addArrangedSubview(descriptionBarStackView)
viewWithStackViews.addArrangedSubview(thirdTextView)

fullView.addSubview(viewWithStackViews)

descriptionBarStackView.translatesAutoresizingMaskIntoConstraints = false
thirdTextView.translatesAutoresizingMaskIntoConstraints = false
viewWithStackViews.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: descriptionBarStackView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30).isActive = true
NSLayoutConstraint.activate([
    viewWithStackViews.topAnchor.constraint(equalTo: fullView.topAnchor, constant: 0),
    viewWithStackViews.leadingAnchor.constraint(equalTo: fullView.leadingAnchor, constant: 0),
    viewWithStackViews.trailingAnchor.constraint(equalTo: fullView.trailingAnchor, constant: 0),
    viewWithStackViews.bottomAnchor.constraint(equalTo: fullView.bottomAnchor, constant: 0),
    ])

fullView.layer.cornerRadius = 5
mainView.addSubview(fullView)
class VisualTestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        let fullView = UIView()
        fullView.backgroundColor = UIColor(red:0.82, green:0.83, blue:0.85, alpha:1.0)
        fullView.translatesAutoresizingMaskIntoConstraints = false

        let firstButton = UIButton()
        firstButton.translatesAutoresizingMaskIntoConstraints = false
        let secondButton = UIButton()
        secondButton.translatesAutoresizingMaskIntoConstraints = false
        let thirdTextView = UITextView()
        thirdTextView.translatesAutoresizingMaskIntoConstraints = false



        thirdTextView.text = "lorem ipsumlorem ipsumlorem ipsumlorem ipsumlorem ipsumlorem ipsumlorem ipsumlorem ipsumlorem ipsum"

        let text1 = "Button1"
        firstButton.setTitle(text1, for: .normal)
        firstButton.setTitleColor(.black, for: .normal)
        firstButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
        firstButton.contentHorizontalAlignment = .left


        secondButton.setTitle("Button2", for: .normal)
        secondButton.setTitleColor(.black, for: .normal)
        secondButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
        secondButton.contentHorizontalAlignment = .right


        let descriptionBarStackView = UIStackView(arrangedSubviews: [firstButton, UIView() ,secondButton])
        descriptionBarStackView.translatesAutoresizingMaskIntoConstraints = false
        descriptionBarStackView.axis = .horizontal
        descriptionBarStackView.alignment = .center

        let viewWithStackViews = UIStackView(arrangedSubviews: [descriptionBarStackView, thirdTextView])
        viewWithStackViews.translatesAutoresizingMaskIntoConstraints = false
        viewWithStackViews.axis = .vertical
        viewWithStackViews.layoutMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        viewWithStackViews.isLayoutMarginsRelativeArrangement = true

        fullView.addSubview(viewWithStackViews)


        fullView.leadingAnchor.constraint(equalTo: viewWithStackViews.leadingAnchor).isActive = true
        fullView.trailingAnchor.constraint(equalTo: viewWithStackViews.trailingAnchor).isActive = true
        fullView.topAnchor.constraint(equalTo: viewWithStackViews.topAnchor).isActive = true
        fullView.bottomAnchor.constraint(equalTo: viewWithStackViews.bottomAnchor).isActive = true

        view.addSubview(fullView)
        fullView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -10).isActive = true
        fullView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        fullView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        thirdTextView.isScrollEnabled = false

        thirdTextView.backgroundColor = .clear
    }


}