Here's a sample of how to create and display a custom view in your code.
I like to implement it without using xib or storyborad, but what's best?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CustumView: UIView { | |
var imageView:UIImageView! | |
var label:UILabel! | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.backgroundColor = .orange | |
imageView = UIImageView() | |
imageView.image = UIImage(named: "tab-icon-sample") | |
imageView.backgroundColor = .red | |
self.addSubview(imageView) | |
label = UILabel() | |
label.text = "Hello" | |
label.backgroundColor = .yellow | |
label.textAlignment = .center | |
self.addSubview(label) | |
} | |
override func layoutSubviews() { | |
let viewWidth = frame.width | |
let viewHeight = frame.height | |
imageView.frame = CGRect(x: 0, y: 0, width: viewWidth * 0.3, height: viewHeight) | |
label.frame = CGRect(x: viewWidth * 0.3, y: 0, width: viewWidth * 0.3, height: viewHeight) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = .white | |
let custumView = CustumView() | |
custumView.frame = CGRect(x: 60, y: 60, width: 300, height: 100) | |
self.view.addSubview(custumView) | |
} | |
} |