Rating Bar In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
 
 
Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to Create a Rating Bar in Xamarin iOS, using Xamarin Studio.

Step 1

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.

 

Step 2

In this step, configure your app. Give the app name (Ex:sample), Organization Identifier. Afterwards, click Next.
 
 

Step 3

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path of your project. Afterwards, click Create.



Step 4

Subsequently, go to the solution. In the solution, get all the files and sources in your project. Now, select Main.storyboard and double click to open Main.storyboard page.

After opening the Main.storyboard, you can design this page, as per your desire.
 
 

Step 5

After opening the Main.storyboard, you can design this page, as per your desire.

 
 
Step 6

In this step, add the Images form your Local system.

Go to Solution—>Resource-->Right click-->Add-->Add Files.Now, you can choose the required Images. Click open.

Next, choose Copy the Files to the directory. Afterwards, click Ok.

 
 
Step 7

In this step design your app.using storyboard, Toolbox and Properties
  1. Button(btn1)
  2. Button(btn2)
  3. Button(btn3)
  4. Button(btn4)
  5. Button(btn5)
  6. Label(lblRate)
 
Step 8

In this step, go to the ViewController.cs page. write the code given below.

ViewController.cs
  1. using System;  
  2. using UIKit;  
  3. namespace XamariniOSRatingBar {  
  4.     public partial class ViewController: UIViewController {  
  5.         protected ViewController(IntPtr handle): base(handle) {  
  6.             // Note: this .ctor should not contain any initialization logic.  
  7.         }  
  8.         public override void ViewDidLoad() {  
  9.             base.ViewDidLoad();  
  10.             btn1.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  11.             btn2.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  12.             btn3.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  13.             btn4.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  14.             btn5.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  15.             lblRating.Text = "Your Rating:0/5";  
  16.         }  
  17.         partial void UIButton17_TouchUpInside(UIButton sender) {  
  18.             btn1.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  19.             btn2.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  20.             btn3.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  21.             btn4.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  22.             btn5.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  23.             lblRating.Text = "Your Rating:5/5";  
  24.         }  
  25.         partial void UIButton16_TouchUpInside(UIButton sender) {  
  26.             btn1.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  27.             btn2.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  28.             btn3.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  29.             btn4.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  30.             btn5.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  31.             lblRating.Text = "Your Rating:4/5";  
  32.         }  
  33.         partial void UIButton15_TouchUpInside(UIButton sender) {  
  34.             btn1.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  35.             btn2.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  36.             btn3.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  37.             btn4.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  38.             btn5.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  39.             lblRating.Text = "Your Rating:3/5";  
  40.         }  
  41.         partial void UIButton14_TouchUpInside(UIButton sender) {  
  42.             btn1.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  43.             btn2.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  44.             btn3.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  45.             btn4.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  46.             btn5.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  47.             lblRating.Text = "Your Rating:2/5";  
  48.         }  
  49.         partial void UIButton13_TouchUpInside(UIButton sender) {  
  50.             btn1.SetBackgroundImage(UIImage.FromFile("yellow-star.png"), UIControlState.Normal);  
  51.             btn2.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  52.             btn3.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  53.             btn4.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  54.             btn5.SetBackgroundImage(UIImage.FromFile("gray-star.png"), UIControlState.Normal);  
  55.             lblRating.Text = "Your Rating:1/5";  
  56.         }  
  57.         public override void DidReceiveMemoryWarning() {  
  58.             base.DidReceiveMemoryWarning();  
  59.             // Release any cached data, images, etc that aren't in use.  
  60.         }  
  61.     }  
  62. }  
 

Step 9

Now, go to Run option , choose Debug and the list of iPhone and iPad simulators which are available. You can choose any one simulator and run it.

 
 
Output

After a few seconds, the app will start running on your iPhone simulator.You will see your app working successfully.

 

You can see the Rating bar is working successfully.

Summary

This was the process of how to create a Rating Bar in Xamarin iOS , using Xamarin Studio.

Please share your comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all