【问题标题】:UITextField is not filing available width inside UIStackViewUITextField 没有在 UIStackView 中归档可用宽度
【发布时间】:2019-06-12 17:07:16
【问题描述】:

我想在屏幕的左右边缘放置两个带有图像的按钮,并在它们之间插入一个UITextField。在storyboard 中一切都很好,但我需要以编程方式进行。

来源:

class SecondViewController: UIViewController {


override func loadView() {
    super.loadView()
    self.view.backgroundColor = .white
    let leftButton = UIButton()
    leftButton.setImage(UIImage(named: "first"), for: .normal)
    leftButton.setTitle("", for: .normal)
    
    let rightButton = UIButton()
    rightButton.setImage(UIImage(named: "second"), for: .normal)
    rightButton.setTitle("", for: .normal)
    
    let textField = UITextField()
    textField.placeholder = "clear"
    textField.borderStyle = .roundedRect
    textField.contentHorizontalAlignment = .left
    textField.contentVerticalAlignment = .center
    
    let stackView = UIStackView()
    
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.alignment = .fill
    stackView.distribution = .fill
    stackView.axis = .horizontal
    stackView.spacing = 15

    stackView.addArrangedSubview(leftButton)
    stackView.addArrangedSubview(textField)
    stackView.addArrangedSubview(rightButton)
    
    self.view.addSubview(stackView)
    
    NSLayoutConstraint(item: stackView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 15).isActive = true
    NSLayoutConstraint(item: stackView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: -15).isActive = true
    NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 50).isActive = true
    
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

我需要像第一张图片一样的结果。

但结果,我获得了第二名。

我做错了什么?

【问题讨论】:

    标签: ios swift uitextfield uikit uistackview


    【解决方案1】:

    你有一个基本的约束歧义。只需切换到 View Debugger,您就可以很容易地发现这一点;你会看到三个感叹号警告你这个问题。布局引擎并不神奇地知道三个视图中的哪一个应该被允许水平拉伸。你必须告诉它:

    textField.setContentHuggingPriority(UILayoutPriority(249), for: .horizontal)
    

    【讨论】:

    • 谢谢。它帮助了我。我在某些 xcode 工具方面受到限制,因为我正在使用 Xamarin.iOS 并将我的问题重新编写为 swift。附言这就是为什么,我仍在使用分号:)
    【解决方案2】:

    设置约束,如

    NSLayoutConstraint.activate([
        stackView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
        stackView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 15),
        stackView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -15),
        stackView.heightAnchor.constraint(equalToConstant: 45),
        leftButton.widthAnchor.constraint(equalToConstant: 30), 
        rightButton.widthAnchor.constraint(equalToConstant: 30),
    ])
    

    另一个小变化

    rightButton.imageView?.contentMode = .scaleAspectFit
    leftButton.imageView?.contentMode = .scaleAspectFit
    

    结果

    【讨论】:

    • 谢谢,这也有效,但我正在寻找@matt 解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多