我是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)
但我不知道如何将约束设置为图像视图以:
a. 居中对齐到父视图的x轴
b. 相对于父视图的宽度比例为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)
revealingSplashView
添加为self.view
的子视图。所以请确保在添加约束之前的某个地方调用self.view.addSubview(revealingSplashView)
。 - undefinedrevealingSplashView
设置约束吗?它也必须有约束...我假设你已经做过了...请更新问题以展示你想要如何约束revealingSplashView
。 - undefined