当应用程序打开时,我试图创建一个自定义广告,弹出一些UIViews和image,然后用两个按钮从我的Firebase中控制它,现在我在以编程方式将adImage和buttonstack(包含两个按钮)添加到我的backView中时遇到了问题,到目前为止没有任何工作。。
ios - 在Swift中以编程方式设置UI约束(UIView中的Image和stackView)-LMLPHP
我需要这张照片拍摄~%75的后视图和剩余的~%25的按钮堆栈
这里有些代码,我已经上传到我的GitHub

import UIKit

class ViewController: UIViewController {

let backroundView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .black
    view.alpha = 0.5
    return view
}()

let backView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.cornerRadius = 15
    return view
}()

let adImage: UIImageView = {
    var image = UIImageView()
    image.translatesAutoresizingMaskIntoConstraints = false
    image.contentMode = .scaleAspectFill
    return image
}()

let buttonsStack: UIStackView = {
    let stack = UIStackView()
    stack.translatesAutoresizingMaskIntoConstraints = false
    stack.alignment = UIStackViewAlignment.fill
    stack.axis = UILayoutConstraintAxis.vertical
    stack.distribution = .equalSpacing
    stack.spacing = 8
    stack.backgroundColor = UIColor.red
    return stack
}()

let actionButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Open", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = UIColor(red: 0, green: 0.60, blue: 1, alpha: 1)
    button.layer.cornerRadius = 8
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.textAlignment = .center
    return button
}()

let dismessButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Exit", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = .lightGray
    button.layer.cornerRadius = 8
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.textAlignment = .center
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
}

func setupUI(){
    // backroundView
    view.addSubview(backroundView)
    backroundView.frame = view.frame
    // backView
    view.addSubview(backView)
    backView.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
    backView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
    backView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
    backView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
    // adImage
    backView.addSubview(adImage)
    adImage.image = UIImage(named: "testImage")
    adImage.topAnchor.constraint(equalTo: backView.topAnchor).isActive = true
    adImage.rightAnchor.constraint(equalTo: backView.rightAnchor).isActive = true
    adImage.leftAnchor.constraint(equalTo: backView.leftAnchor).isActive = true
    adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.50).isActive = true
    // buttonsStack
    buttonsStack.addArrangedSubview(actionButton)
    buttonsStack.addArrangedSubview(dismessButton)
    backView.addSubview(buttonsStack)
    buttonsStack.topAnchor.constraint(equalTo: backView.topAnchor, constant: 15).isActive = true
    buttonsStack.bottomAnchor.constraint(equalTo: backView.bottomAnchor, constant: -15).isActive = true
    buttonsStack.rightAnchor.constraint(equalTo: backView.rightAnchor, constant: -15).isActive = true
    buttonsStack.leftAnchor.constraint(equalTo: backView.leftAnchor, constant: 15).isActive = true
}
}

最佳答案

如果图像为0.75,请更改此项

adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.50).isActive = true


adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.75).isActive = true

//
然后扣子应该在下面,所以改变这个
buttonsStack.topAnchor.constraint(equalTo: backView.topAnchor, constant: 15).isActive = true


buttonsStack.topAnchor.constraint(equalTo: adImage.bottomAnchor, constant: 15).isActive = true

关于ios - 在Swift中以编程方式设置UI约束(UIView中的Image和stackView),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51115880/

10-13 04:34