代码之家  ›  专栏  ›  技术社区  ›  Vyachaslav Gerchicov

UIButton已调整大小以适合UIImage

  •  0
  • Vyachaslav Gerchicov  · 技术社区  · 6 年前
        let btn = UIButton(frame: CGRect.zero)
        btn.setImage(..., for: UIControlState.normal)
        addSubview(btn)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.rightAnchor.constraint(equalTo: btn.superview!.rightAnchor, constant: -32).isActive = true
        btn.topAnchor.constraint(equalTo: btn.superview!.topAnchor, constant: 6 + vc.view.safeAreaInsets.top).isActive = true
        btn.widthAnchor.constraint(equalToConstant: 32)
        btn.heightAnchor.constraint(equalToConstant: 32)
    
        btn.backgroundColor = .black //to check the rect
    

    图像的大小是16x16。按钮应至少大两倍(因为接触面积小)。

    width==16 height==22 . 同时,距离约束正常工作。

    那我犯了什么错?或者我必须使用适合按钮1:1的图像资源?

    1 回复  |  直到 6 年前
        1
  •  0
  •   rounak    6 年前

    btn.setContentHuggingPriority(.defaultLow, for: .horizontal)
    btn.setContentHuggingPriority(.defaultLow, for: .vertical)