How do you create a UIImage View Programmatically - Swift(如何以编程方式创建 UIImage 视图 - Swift)
问题描述
我正在尝试以编程方式创建 UIImage 视图,我有一个新视图并尝试这样做
I'm trying to create a UIImage View programmatically, I have a new view and I tried doing this
let imageName = "yourImage.png"
yourview.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName))
这不起作用,因为我不知道第二行中的 yourview 应该是什么.
this did not work because I don't know what this should be yourview in the second line.
问题:如何通过编码而不是在情节提要中进行 UIImageView 显示在屏幕上
Question: How do I make a UIImageView appear on the screen by coding it instead of doing it in the storyboard
推荐答案
首先你从你的图像文件创建一个 UIImage,然后从它创建一个 UIImageView:
First you create a UIImage from your image file, then create a UIImageView from that:
let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
最后,您需要给 imageView 一个框架并将其添加到您的视图中以使其可见:
Finally you'll need to give imageView a frame and add it your view for it to be visible:
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)
                        这篇关于如何以编程方式创建 UIImage 视图 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式创建 UIImage 视图 - Swift
				
        
 
            
        基础教程推荐
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				