简体   繁体   中英

Xamarin Ios - Create rounded buttons on only 1 side

我正在为Xamarin Ios开发一个应用程序,我正在努力找到一种方法,只需将一个圆形边框应用于UIButton类型的按钮。

In your UIButton subclass, override the LayoutSubviews method and add a mask:

Example: Top and bottom of left side rounded:

public override void LayoutSubviews()
{
    var maskingShapeLayer = new CAShapeLayer()
    {
        Path = UIBezierPath.FromRoundedRect(Bounds, UIRectCorner.BottomLeft | UIRectCorner.TopLeft, new CGSize(20, 20)).CGPath
    };
    Layer.Mask = maskingShapeLayer;
    base.LayoutSubviews();
}

在此输入图像描述

You can do this (IOS 11.0+):

yourLabel.Layer.CornerRadius = 5; // set radius on all corners
yourLabel.ClipsToBounds = true; 
yourLabel.Layer.MaskedCorners = (CoreAnimation.CACornerMask)1; // cast the correct value as CACornerMask enum

As CoreAnimation.CACornerMask is an enum marked as Flags and has only 4 values defined (1,2,4,8), I assumed that you can do bitwise operations there but that didn't work for me... So the only way is to cast it with a correct value like this:

yourLabel.Layer.MaskedCorners = (CoreAnimation.CACornerMask)5;  //top & bottom left corners rounded

Pick your value from this list based on which corners do you want to be rounded:

  • 0: no rounded corners
  • 1: top left
  • 2: top right
  • 3: top left & right (both top corners)
  • 4: bottom left
  • 5: top & bottom left (both left corners)
  • 6: top right & bottom left
  • 7: top left & right, bottom left (all corners except bottom right)
  • 8: bottom right
  • 9: top left, bottom right
  • 10: top & bottom right (both right corners)
  • 11: both top corners, bottom right (all corners except bottom left)
  • 12: bottom left & right (both bottom corners)
  • 13: bottom left & right, top left (all corners except top right)
  • 14: bottom left & right, top right (all corners except top left)
  • 15: all corners rounded

That does the trick...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM