[英]mapKit error as Value of type 'UIImageView' has no member 'anchor'
var imageView: UIImageView {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.image = UIImage(named: "locationPin")
return iv
}
//MARK:- Init
override func viewDidLoad() {
super.viewDidLoad()
configureViewComponents()
}
//MARK:- Helper function
func configureViewComponents() {
view.backgroundColor = .white
view.addSubview(imageView)
// below I am getting error as
Value of type 'UIImageView' has no member 'anchor' 'UIImageView' 类型的值没有成员 'anchor'
imageView.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 140, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 200, height: 200)
}
}
I tried to implement the popup enable location screen programmatically with an imageview but I am getting the error as "Value of type 'UIImageView' has no member 'anchor'"我尝试使用 imageview 以编程方式实现弹出式启用位置屏幕,但我收到错误消息“'UIImageView' 类型的值没有成员'锚'”
You should have你应该有
extension UIView {
func anchor(} {}
}
imageViewtranslatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 140.0 ),
imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10 ),
imageView.widthAnchor.constraint(equalToConstant:200),
imageView.heightAnchor.constraint(equalToConstant: 200)
])
With extension带扩展
enum ConstraintType {
case top, leading, trailing, bottom, width, height
}
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
//translate the view's autoresizing mask into Auto Layout constraints
translatesAutoresizingMaskIntoConstraints = false
var constraints: [ConstraintType : NSLayoutConstraint] = [:]
if let top = top {
constraints[.top] = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
constraints[.leading] = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
constraints[.bottom] = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
constraints[.trailing] = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
constraints[.width] = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
constraints[.height] = heightAnchor.constraint(equalToConstant: size.height)
}
let constraintsArray = Array<NSLayoutConstraint>(constraints.values)
NSLayoutConstraint.activate(constraintsArray)
}
}
class ViewController: UIViewController {
let imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.image = UIImage(named: "locationPin")
return iv
}()
// MARK: - View Life Cycle Methods
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.anchor(top:view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: nil, padding: UIEdgeInsets.init(top: 140, left: 0, bottom: 0, right: 0), size: CGSize.init(width: 200, height: 200))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.