如何在我的应用程序崩溃的情况下向UIView实例的子视图添加约束?

时间:2019-04-04 10:41:28

标签: swift uiview

我在Swift中有一个UIView实例,在其中添加了UIImageView实例的子视图,但是在我的应用程序崩溃之前,我无法向UIImageView实例添加约束。

我尝试用Xcode 10.2和iOS 12.2编写Swift 5.0代码来设置约束,使UIImageView实例的前端与UIView实例的左侧边缘相距50。

public async Task<JsonResult> ActionMethod(string selectedType, List<AdvanceFilterQuery> selectedValue, string operatorType)
{

}

我希望包含UIImageView实例的UIView实例位于UIView实例右侧的50。相反,应用程序在Xcode中崩溃,并在文件AppDelegate.swift中显示错误消息“线程1:信号SIGABRT”。

3 个答案:

答案 0 :(得分:0)

这行吗?

.modalBackground

答案 1 :(得分:0)

此代码应该有效:

cur.area > 500 && cur.area =< 1000

答案 2 :(得分:0)

首先,您必须将图像添加到父视图

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderView: UIView = UIView()

    let sectionHeaderImage: UIImage = UIImage(named: "Flag - ENGLAND.png")!
    let sectionHeaderImageView: UIImageView = UIImageView(image: sectionHeaderImage)

        sectionHeaderImageView.translatesAutoresizingMaskIntoConstraints = false

        sectionHeaderView.addSubview(sectionHeaderImageView)

        let imageViewHeightConstraint = NSLayoutConstraint(
            item: sectionHeaderImageView,
            attribute: .height,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 40
        )
        let imageViewWidthConstraint = NSLayoutConstraint(
            item: sectionHeaderImageView,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 40
        )
        let imageViewLeadingConstraint = NSLayoutConstraint(
            item: sectionHeaderImageView,
            attribute: .left,
            relatedBy: .equal,
            toItem: sectionHeaderView,
            attribute: .left,
            multiplier: 1.0,
            constant: 50
        )
        sectionHeaderView.addConstraints([
            imageViewHeightConstraint,
            imageViewWidthConstraint,
            imageViewLeadingConstraint
            ])
    }
    return sectionHeaderView
}