【问题标题】:how to add image view with some constraints programmatically using swift?如何使用 swift 以编程方式添加具有一些约束的图像视图?
【发布时间】:2019-04-30 08:08:20
【问题描述】:

我是 iOS 开发的新手。如果使用故事板,我可以像这样在视图控制器中放置一个图像视图

我需要以编程方式制作类似的东西。

所以我有一个名为 RevealingSplashView 的库中的自定义视图,但我需要向该自定义 UI 视图添加一个图像视图。我只知道添加图像视图,可能是这样的

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
revealingSplashView.addSubview(imageView)

但我不知道如何将该约束设置为图像视图

一个。对齐到中心 x 到 superview

b.与 superview 0.8 的比例宽度

c。高度限制 = 25

d。将底部对齐到安全区域 = 32

怎么做?

这是我之前想要添加图像视图的代码

let screenSize = UIScreen.main.bounds
            let iconWidth = (screenSize.width) * 0.8
            let iconHeight = iconWidth * 1 // ratio 1:1
            revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "Loading Page Asset")!,iconInitialSize: CGSize(width: iconWidth, height: iconHeight), backgroundColor: AppColor.mainYellow.getUIColor())
            revealingSplashView.animationType = SplashAnimationType.twitter
            revealingSplashView.imageView?.contentMode = .scaleAspectFill


            // add loading indicator to RevealingSplashView Programatically
            revealingSplashViewIndicator.color = UIColor.white
            revealingSplashViewIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
            revealingSplashViewIndicator.center =  CGPoint(x: self.view.center.x, y: self.view.center.y + (iconHeight/2) + 64 )
            revealingSplashView.addSubview(revealingSplashViewIndicator)
            revealingSplashViewIndicator.bringSubviewToFront(self.revealingSplashView)
            revealingSplashViewIndicator.startAnimating()

            let window = UIApplication.shared.keyWindow
            window?.addSubview(revealingSplashView)

【问题讨论】:

    标签: ios swift autolayout


    【解决方案1】:

    我推荐使用锚点:

    let imageName = "yourImage.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)
    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    revealingSplashView.addSubview(imageView)
    
    // you need to turn off autoresizing masks (storyboards do this automatically)
    imageView.translatesAutoresizingMaskIntoConstraints = false
    
    // setup constraints, it is recommended to activate them through `NSLayoutConstraint.activate`
    // instead of `constraint.isActive = true` because of performance reasons
    NSLayoutConstraint.activate([
        imageView.centerXAnchor.constraint(equalTo: revealingSplashView.centerXAnchor),
        imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8),
        imageView.heightAnchor.constraint(equalToConstant: 25),
        imageView.bottomAnchor.constraint(equalTo: revealingSplashView.safeAreaLayoutGuide.bottomAnchor, constant: -32),
        ])
    

    【讨论】:

    • 不幸的是,它使我的应用程序崩溃:*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚激活约束 <0x283c9b7c0><0x283c36e40>
    【解决方案2】:

    要以编程方式添加约束,您需要设置

    imageView.translatesAutoresizingMaskIntoConstraints = false
    

    然后你可以设置约束:

    imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    

    如果你想激活你需要设置的约束

    isActive = true
    

    如果你支持 iOS

    【讨论】:

    • 不幸的是,它使我的应用程序崩溃,错误消息:*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚激活约束 <0x280d18f80 width><0x280d18cc0>
    【解决方案3】:
    revealingSplashView.addSubview(imageView)
    
    imageView.centerXAnchor.constraint(equalTo: revealingSplashView.centerXAnchor).isActive = true
    imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2019-06-25
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      相关资源
      最近更新 更多