Xamarin iOS Tutorial Custom view Xamarin iOS custom view
The views in the toolbar are much used in the actual application development, but in order to attract the user's eyeballs, developers can make some custom views.
Example 2-33 below will implement a custom view. When the user touches the screen, a tabbed view showing the current position of the finger appears, and the background color of the primary view is changed. The code is as follows:
(1 Create a single View application type of project named 2-13.
(2 Add a C # class file, named MyView, with the following steps:
First, select the file in the menu bar | new| File ... command, pop up the new File dialog box, shown in 2.53.
Figure 2.53 Operation steps 1
Then select the empty class in general, and after you enter the name of the class, click the New button, and a class file named MyView is created.
(3 Open the Mainstoryboard.storyboard file, select the front view, select the right-most properties button, and in the Properties dialog box, set class to the class file name MyView you created. As shown in 2.54.
Figure 2.54 Operation steps 2
(4 Open the MyView.cs file, write your code, and implement a custom view. The code is as follows:
- Using System;
- Using System.Drawing;
- Using Monotouch.foundation;
- Using Monotouch.uikit;
- Using System.CodeDom.Compiler;
- Namespace Application
- {
- Partial class Myview:uiview
- {
- Private UILabel labelstatus;
- Public MyView (INTPTR handle): Base (handle)
- {
- This. Initialize ();
- }
- Public MyView (RectangleF frame): Base (frame)
- {
- This. Initialize ();
- }
- Initialize method
- private void Initialize ()
- {
- This. BackgroundColor = Uicolor.lightgray;
- //Add a Label object
- Labelstatus = new UILabel (new RectangleF (0f, 0f, this. Frame.width, 60f));
- Labelstatus.textalignment = Uitextalignment.center;
- Labelstatus.backgroundcolor = Uicolor.darkgray;
- Labelstatus.textcolor = Uicolor.white;
- This. Addsubview (This.labelstatus);
- }
- //Implement Touch events
- Public override void Touchesmoved (Nsset touches, uievent evt)
- {
- Base. Touchesmoved (touches, evt);
- Uitouch touch = (Uitouch) touches. Anyobject;
- PointF touchlocation = touch. Locationinview (this); //Get the current position of the touch point
- Labelstatus.text = String.Format ("X: {0}-Y: {1}", touchlocation.x, TOUCHLOCATION.Y);
- }
- }
- }
Run effect 2.55 as shown.
Figure 2.55 Running effect
Note: The following constructor overrides the base class's UIView (IntPtr) constructor, which is always used as a view initialized with localized code.
- Public MyView (RectangleF frame): Base (frame) {}
The Touchesmoved () method is overridden to execute the contents of this method when the user's finger moves on the main view.
Xamarin IOS modifies the same view at once
In one application, many of the same views are used. What if you want to change the properties of these views and the properties are the same? Perhaps a smart developer would think that it would be nice to first write the changed property in a View object and then copy it, preferably by changing the object name of the property.
Such a method is feasible, but it only works with a small number of view objects. Is this method feasible if there are hundreds or thousands of identical view objects in this application? Of course it's not possible, which makes the code look redundant and takes developers a long time. So is there a way to modify the same properties of the same view at once? The answer is now positive. This can be accomplished using the Appearance property, which is a type method with the following syntax:
- The view class. Appearance. Properties of the View = property settings;
The code under "Example 2-34" uses the Appearance property to change all the labels in the main view to a cyan background with a brown color title. The code is as follows:
- Using System;
- Using System.Drawing;
- Using Monotouch.foundation;
- Using Monotouch.uikit;
- Namespace Application
- {
- public partial class __14viewcontroller:uiviewcontroller
- {
- ...//This omits the construction method and the destructor method of the view controller.
- #region View Lifecycle
- public override void Viewdidload ()
- {
- Base. Viewdidload ();
- Perform any additional setup after loading the view, typically from a nib.
- Add Caption Object Label1
- UILabel Label1 = new UILabel ();
- Label1. Frame = new RectangleF (0, 90, 320, 50);
- Label1. text= "Red";
- This. View.addsubview (Label1);
- Add Caption Object Label1
- UILabel label2 = new UILabel ();
- Label2. Frame = new RectangleF (0, 200, 320, 50);
- Label2. text= "Yellow";
- This. View.addsubview (Label2);
- Add Caption Object Label1
- UILabel label3 = new UILabel ();
- Label3. Frame = new RectangleF (0, 310, 320, 50);
- Label3. text= "Cyan";
- This. View.addsubview (LABEL3);
- Add Caption Object Label1
- UILabel label4 = new UILabel ();
- Label4. Frame = new RectangleF (0, 420, 320, 50);
- Label4. Text= "Blue";
- This. View.addsubview (LABEL4);
- }
- ...//This omits some methods before and after view loading and unloading
- #endregion
- }
- }
Run effect 2.56 as shown.
- UILabel.Appearance.BackgroundColor = Uicolor.cyan; //Set the background of all labels
- UILabel.Appearance.TextColor = Uicolor.brown; //Set text color for all labels
Run effect 2.57 as shown.
Figure 2.56 Run effect figure 2.57 run effect
This article is selected from: Xamarin iOS Development Combat University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!
Custom view of the Xamarin iOS tutorial