5 UIImageView

Swift 3.0 // // ViewController.swift // UIKit005 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController { private var myImageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // UIImageViewのサイズを設定する let iWidth: CGFloat = 300 let iHeight: CGFloat = 100 // UIImageViewのx,yを設定する let posX: CGFloat = (self.view.bounds.width - iWidth)/2 let posY: CGFloat = (self.view.bounds.height - iHeight)/2 // UIImageViewを作成. myImageView = UIImageView(frame: CGRect(x: posX, y: posY, width: iWidth, height: iHeight)) // UIImageを作成. let myImage: UIImage = UIImage(named: "fabo_logo.png")! // 画像をUIImageViewに設定する. myImageView.image = myImage // UIImageViewをViewに追加する self.view.addSubview(myImageView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } Swift 2.3 // // ViewController.swift // UIKit005 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController { private var myImageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // UIImageViewのサイズを設定する let iWidth: CGFloat = 300 let iHeight: CGFloat = 100 // UIImageViewのx,yを設定する let posX: CGFloat = (self.view.bounds.width - iWidth)/2 let posY: CGFloat = (self.view.bounds.height - iHeight)/2 // UIImageViewを作成. myImageView = UIImageView(frame: CGRectMake(posX, posY, iWidth, iHeight)) // UIImageを作成. let myImage: UIImage = UIImage(named: "fabo_logo.png")! // 画像をUIImageViewに設定する. myImageView.image = myImage // UIImageViewをViewに追加する self.view.addSubview(myImageView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 2.3と3.0の差分 CGRectMake()がCGRect()に変更. Reference UIImageView https://developer.apple.com/reference/uikit/uiimageview UIImage https://developer.apple.com/reference/uikit/uiimage

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.