This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathButtonAccessoryView.cs
More file actions
67 lines (58 loc) · 2.61 KB
/
ButtonAccessoryView.cs
File metadata and controls
67 lines (58 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using UIKit;
using CoreGraphics;
using System.Collections.Generic;
namespace CodeHub.iOS.Views
{
public class ButtonAccessoryView : UIView
{
private readonly ScrollingToolbarView _scrollingToolBar;
public ButtonAccessoryView(IEnumerable<UIButton> buttons)
{
Frame = new CGRect(0, 0, 320f, 44f);
var normalButtonImage = ImageFromColor(UIColor.White);
var pressedButtonImage = ImageFromColor(UIColor.FromWhiteAlpha(0.0f, 0.4f));
foreach (var button in buttons)
{
button.SetBackgroundImage(normalButtonImage, UIControlState.Normal);
button.SetBackgroundImage(pressedButtonImage, UIControlState.Highlighted);
}
_scrollingToolBar = new ScrollingToolbarView(new CGRect(0, 0, Bounds.Width, Bounds.Height), buttons);
_scrollingToolBar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
_scrollingToolBar.BackgroundColor = UIColor.FromWhiteAlpha(0.84f, 1.0f);
Add(_scrollingToolBar);
}
public static UIButton CreateAccessoryButton(UIImage image, Action action)
{
var btn = CreateAccessoryButton(string.Empty, action);
btn.SetImage(image, UIControlState.Normal);
btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
btn.ImageEdgeInsets = new UIEdgeInsets(6, 6, 6, 6);
return btn;
}
public static UIButton CreateAccessoryButton(string title, Action action)
{
var fontSize = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone ? 22 : 28f;
var btn = new UIButton(UIButtonType.System);
btn.Frame = new CGRect(0, 0, 26, 32);
btn.SetTitle(title, UIControlState.Normal);
btn.BackgroundColor = UIColor.White;
btn.Font = UIFont.BoldSystemFontOfSize(fontSize);
btn.Layer.CornerRadius = 7f;
btn.Layer.MasksToBounds = true;
btn.AdjustsImageWhenHighlighted = false;
btn.TouchUpInside += (sender, e) => action();
return btn;
}
private static UIImage ImageFromColor(UIColor color)
{
UIGraphics.BeginImageContext(new CGSize(1, 1));
var context = UIGraphics.GetCurrentContext();
context.SetFillColor(color.CGColor);
context.FillRect(new CGRect(0, 0, 1, 1));
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
}
}