This framework allows iOS developers to add a rating control to their app as shown in the demo section.
- iOS 8.0 or higher
-
Change directory to your project directory
cd project-directory
-
Initialize the directory with CocoaPods. This step will create a
Podfile
file where you will specify your dependencies.pod init
-
Open the
Podfile
file in a text editor.nano Podfile
-
Add the following line to the
Podfile
pod 'ARKRating'
-
Save the file. Then run
pod install
Copy the Source folder of the project ARKRating
to your project.
This repo contains an example project named ARKRatingExample
for illustration purposes. You can either use the storyboard to add this widget to your app or add it in code.
-
Go to
Main.storyboard
-
Add a
UIView
from theObject Library
-
Change its class to
ARKRating
in the class inspector tab -
Adjust the configurable properties as needed:
- Max Rating
- Current Rating
- Rating Border Color
- Rating Border Width
- Rating Color
- Rating Inner Color
First, import the library into a view controller.
import ARKRating
Create a property of type ARKRatingControl
.
let ratingControl: ARKRatingControl = {
// Create an instance of ARKRatingControl
let ratingControl = ARKRatingControl()
// Set this to false if using Autolayout
ratingControl.translatesAutoresizingMaskIntoConstraints = false
// Configurable properties
// Number of items in the widget
// ratingControl.maxRating = 5
// Current rating
// ratingControl.currentRating = 2.5
// Color properties
// ratingControl.ratingBorderColor = .darkGray
// ratingControl.ratingBorderWidth = 1
// ratingControl.ratingColor = .orange
// ratingControl.ratingInnerColor = .lightGray
// Add a target to receive state change updates
ratingControl.addTarget(self, action: #selector(ratingControlValueChanged), for: .valueChanged)
return ratingControl
}()
Add a callback function to receive state change updates.
@objc func ratingControlValueChanged(){
print(self.ratingControl.currentRating)
}
Add the control to the view.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Add to view
self.view.addSubview(self.ratingControl)
// Setup autolayout constraints or frame
self.ratingControl.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self.ratingControl.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
self.ratingControl.heightAnchor.constraint(equalToConstant: 40).isActive = true
self.ratingControl.widthAnchor.constraint(equalToConstant: 240).isActive = true
// or do
// self.ratingControl.frame = CGRect(x: 0, y: 0, width: 240, height: 40)
}
That's it. You now have a highly configurable rating control in your app.
- Aswin Raj Kharel
Feel free to contribute to this repo or open an issue to request features or report bugs.