约束锚在Swift 4中运行不正常

时间:2017-10-12 01:24:08

标签: swift constraints swift4

我似乎无法弄清楚为什么约束锚不起作用

inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
inputsContainerView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 150).isActive = true

我试过到处寻找,但似乎无法找到问题。

我没有看到任何冲突的约束或警告,基本上我需要容器看起来像位于屏幕中心的白色矩形。

let inputsContainerView = UIView()
inputsContainerView.backgroundColor = UIColor.white
inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputsContainerView )

这就是它的样子:

This is what it looks like

这应该是这样的:

This is what it should look like

1 个答案:

答案 0 :(得分:4)

您希望150的身高view.height + 150。现在,您将其设置为heightAnchor

您需要将inputsContainerView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 150).isActive = true 设置为常数:

更改此约束:

inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true

到此:

public class GenderToImageConverter : IValueConverter
{
    public object Convert(object value, object parameter, CultureInfo cultureInfo)
   {
        Gender gender = (Gender)value;
        if(gender == Gender.Female)
            return "male.png";
        else
            return "female.png";
   }
}