Hello, Devs!
On the last years with my team, were are using Storyboards.
From now on we get started with View Code.
I was writing the and learning the best way to do that and I have some doubts around it.
In particular case I writing something like that (Is not done from while)
import UIKit
class TopView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupTopView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupTopView() {
self.addSubview(contentTopView )
}
let contentTopView: UIView = {
let topView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
topView.backgroundColor = .gray
topView.layer.cornerRadius = 12
topView.layer.shadowRadius = 1.0
topView.layer.shadowOpacity = 0.5
topView.layer.shadowColor = UIColor.lightGray.cgColor
topView.layer.shadowOffset = CGSize(width: 0, height: 10)
return topView
}()
}
This is the best way to write View Code? Why I should to use () on the final block of the contentToTopView after the }?
Thanks,
Cheers!!!
Post
Replies
Boosts
Views
Activity
Hello from NY o/
On the many projects, I was working out with Storyboards.
From now on, I'm beginning with View Code and I'm trying to change the size of the UIView as something like that: e.g
class ViewController: UIViewController {
// MARK: - Subviews
private lazy var containerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
view.frame.size.height = 200.0
view.frame.size.width = 300.0
view.sizeToFit()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(containerView)
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
And the size of the View is not sizing. I need use CGRect to change the size of the UIView? But how to center with auto layout when CGRect(x: 0, y: 0, height: 200, width: 350)?
The x & y stay to hard to keep center x & center y at any screen size?
Thanks a lot for you help.
Cheers.
Hello Devs!! Hello from NY o/
On this particular case, I'm make some studying about Dependency Injection and I'm just making a couple mockups to improve my lessons.
By the way, I would like understand more deep why the ...{ coder in ... at my closure be represented a casting with my another class as something like this below:
...
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Movie"
}
@IBAction func inject(_ sender: Any) {
let movie = Movie(title: "GoldenEye 007", genre: "Action", poster: imageLiteral(resourceName: "007 Goldeneye"))
guard let moviesDetailsVC = self.storyboard?.instantiateViewController(identifier: "ShowMore", creator: { coder in
return MoviewsDetailViewController(coder: coder, movie: movie)
}) else {
fatalError("MoviesViewController has not been implemented")
}
self.navigationController?.pushViewController(moviesDetailsVC, animated: true)
}
}
...
That's my another class:
import UIKit
class MoviewsDetailViewController: UIViewController {
@IBOutlet weak var posterImageView: UIImageView!
var movie: Movie
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = movie.title
self.posterImageView.image = movie.poster
}
init?(coder: NSCoder, movie: Movie) {
self.movie = movie
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Cheers!!!