Last active
April 3, 2020 22:43
-
-
Save kipters/ea3a851a3106879aea7f747e38758104 to your computer and use it in GitHub Desktop.
UIView subclass and UIViewAppearance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CoreGraphics; | |
using Foundation; | |
using ObjCRuntime; | |
using UIKit; | |
namespace App1 | |
{ | |
[Register("CustomView")] | |
public class CustomView : UIView | |
{ | |
public CustomView() | |
{ | |
Initialize(); | |
} | |
public CustomView(CGRect bounds) : base(bounds) | |
{ | |
Initialize(); | |
} | |
void Initialize() | |
{ | |
} | |
public new static CustomViewAppearance Appearance | |
{ | |
get | |
{ | |
var appearance = GetAppearance<CustomView>(); | |
return new CustomViewAppearance(appearance.Handle); | |
} | |
} | |
public UIColor FuffaColor | |
{ | |
[Export("fuffaColor")] | |
get => BackgroundColor; | |
[Export("setFuffaColor:")] | |
set => BackgroundColor = value; | |
} | |
public nfloat Radius | |
{ | |
get => IntRadius.NFloatValue; | |
set => IntRadius = NSNumber.FromNFloat(value); | |
} | |
private NSNumber IntRadius | |
{ | |
[Export("__radius")] | |
get => NSNumber.FromNFloat(Layer.CornerRadius); | |
[Export("__setRadius:")] | |
set | |
{ | |
Layer.CornerRadius = value.NFloatValue; | |
ClipsToBounds = Layer.CornerRadius > 0; | |
} | |
} | |
public class CustomViewAppearance : UIViewAppearance | |
{ | |
private static readonly Selector FuffaColorGetter = new Selector("fuffaColor"); | |
private static readonly Selector FuffaColorSetter = new Selector("setFuffaColor:"); | |
private static readonly Selector RadiusGetter = new Selector("__radius"); | |
private static readonly Selector RadiusSetter = new Selector("__setRadius:"); | |
protected internal CustomViewAppearance(IntPtr handle) : base(handle) | |
{ | |
} | |
public UIColor FuffaColor | |
{ | |
get => PerformSelector(FuffaColorGetter) as UIColor; | |
set => PerformSelector(FuffaColorSetter, value); | |
} | |
public nfloat Radius | |
{ | |
get => ((NSNumber) PerformSelector(RadiusGetter)).NFloatValue; | |
set => PerformSelector(RadiusSetter, NSNumber.FromNFloat(value)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment