A Dynamic Star Rating View in Pure Swift by R-H-T
This view can be initialized either by code or be included in your storyboard or nib.
In Code:
// MARK: - Methods
fileprivate func setupStarRatingView() {
let starRatingView = StarRatingView(starCount: 5, rating: 5)
view.addSubview(starRatingView)
starRatingView.hasDropShadow = true
starRatingView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
starRatingView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0),
starRatingView.heightAnchor.constraint(equalToConstant: 48),
starRatingView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
starRatingView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0)
])
self.starRatingView = starRatingView
}
For Storyboards:
// ViewController.swift
/* ... */
// MARK: - Properties
// MARK: Outlets
@IBOutlet weak var starRatingView: StarRatingView!
/* ... */
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
starRatingView.hasDropShadow = true
starRatingView.updateView(rating: 4.5)
}
You'll need to provide three images in your assets folder representing each
of the star's 3 main states (emptyStar
, halfStar
, filledStar
).
Note that the images must be somewhat clean and simple, because they will be rendered as templates by default and tinted to UIColor.yellow
.
TBD (To be determined)
Copyright © 2018 Roberth Hansson-Tornéus. All rights reserved.