タイトルの通りですが、以下の記事(Swift)を参考にしてXamarin版のUIButton拡張クラスを作ってみたのですが、領域が広がるどころか狭くなってしまいました。何か間違っているでしょうか?
c#
1 class UIButtonEx : UIButton 2 { 3 public UIEdgeInsets Insets { get; set; } = new UIEdgeInsets(0, 0, 0, 0); 4 5 public UIButtonEx(CGRect frame) : base(frame) 6 { 7 } 8 9 public override bool PointInside(CGPoint point, UIEvent uievent) 10 { 11 CGRect rect = this.Bounds; 12 rect.X -= Insets.Left; 13 rect.Y -= Insets.Top; 14 CGSize size = rect.Size; 15 size.Width += Insets.Left + Insets.Right; 16 size.Height += Insets.Top + Insets.Bottom; 17 rect.Size = size; 18 19 // 拡大したViewサイズがタップ領域に含まれているかどうかを返します 20 return rect.Contains(point); 21 } 22 }
▼呼び出し側
c#
1 private UIBarButtonItem CreateButton(string iconName, OnButtonClicked action, OnButtonLongPressed longPressAction) 2 { 3 UIButtonEx button = new UIButtonEx(new CGRect(0, 0, 20, 20)); 4 button.Insets = new UIEdgeInsets(10, 10, 10, 10); // タップ領域を拡張する 5 UIImage backgroundImage = UIImage.FromBundle(iconName).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); 6 button.SetBackgroundImage(backgroundImage, UIControlState.Normal); 7 button.TouchUpInside += (s, e) => action(s, e);
◆環境
▼Windows 10
Visual Studio Community 2017 Version 15.9.11
Xamarin 4.12.3.83
Xamarin.iOS and Xamarin.Mac SDK 12.4.0.64
▼Mac mini(Late 2014)
macOS Majave 10.14.4
Xcode 10.2.1
あなたの回答
tips
プレビュー