Skip to content

Instantly share code, notes, and snippets.

@kipters
Last active April 3, 2020 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kipters/ea3a851a3106879aea7f747e38758104 to your computer and use it in GitHub Desktop.
Save kipters/ea3a851a3106879aea7f747e38758104 to your computer and use it in GitHub Desktop.
UIView subclass and UIViewAppearance
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