зеркало из https://github.com/xamarin/ios-samples.git
[Chat] setup toolbar with code. It is imposible to setup autolayout on uibarbuttonitems via XCoder or XS designer, so the code is the only way
Xamarin.iOS 8.10.0.255
This commit is contained in:
Родитель
146d4338db
Коммит
c72f3c07b7
|
@ -7,24 +7,35 @@ namespace Chat
|
||||||
{
|
{
|
||||||
public partial class ChatInputView : UIView
|
public partial class ChatInputView : UIView
|
||||||
{
|
{
|
||||||
|
static readonly UIColor ButtonTextColorNormal = UIColor.FromRGB (1, 122, 255);
|
||||||
|
static readonly UIColor ButtonTextColorDisabled = UIColor.FromRGB (142, 142, 147);
|
||||||
|
static readonly UIFont ButtonFont = UIFont.SystemFontOfSize (17, UIFontWeight.Bold);
|
||||||
|
|
||||||
|
static readonly UIColor InputBackgroundColor = UIColor.FromWhiteAlpha (250, 1);
|
||||||
|
static readonly UIColor InputBorderColor = UIColor.FromRGB (200, 200, 205);
|
||||||
|
const float BorderWidth = 0.5f;
|
||||||
|
const float CornerRadius = 5;
|
||||||
|
|
||||||
|
const float ToolbarMinHeight = 44;
|
||||||
|
|
||||||
public UITextView TextView { get; private set; }
|
public UITextView TextView { get; private set; }
|
||||||
public UIButton SendButton { get; private set; }
|
public UIButton SendButton { get; private set; }
|
||||||
|
|
||||||
public ChatInputView()
|
public ChatInputView()
|
||||||
{
|
{
|
||||||
TextView = new UITextView ();
|
TextView = new UITextView ();
|
||||||
TextView.Layer.BorderColor = UIColor.FromRGB (200, 200, 205).CGColor;
|
TextView.Layer.BorderColor = InputBorderColor.CGColor;
|
||||||
TextView.Layer.BorderWidth = (float)0.5;
|
TextView.Layer.BorderWidth = BorderWidth;
|
||||||
TextView.Layer.CornerRadius = 5;
|
TextView.Layer.CornerRadius = CornerRadius;
|
||||||
TextView.BackgroundColor = UIColor.FromWhiteAlpha (250, 1);
|
TextView.BackgroundColor = InputBackgroundColor;
|
||||||
TextView.TranslatesAutoresizingMaskIntoConstraints = false;
|
TextView.TranslatesAutoresizingMaskIntoConstraints = false;
|
||||||
|
|
||||||
SendButton = new UIButton ();
|
SendButton = new UIButton ();
|
||||||
SendButton.SetTitle ("Send", UIControlState.Normal);
|
SendButton.SetTitle ("Send", UIControlState.Normal);
|
||||||
SendButton.Font = UIFont.SystemFontOfSize (15, UIFontWeight.Bold);
|
SendButton.Font = ButtonFont;
|
||||||
|
SendButton.SetTitleColor (ButtonTextColorNormal, UIControlState.Normal);
|
||||||
|
SendButton.SetTitleColor (ButtonTextColorDisabled, UIControlState.Disabled);
|
||||||
SendButton.TranslatesAutoresizingMaskIntoConstraints = false;
|
SendButton.TranslatesAutoresizingMaskIntoConstraints = false;
|
||||||
SendButton.SetTitleColor (UIColor.FromRGB (142, 142, 147), UIControlState.Disabled);
|
|
||||||
SendButton.SetTitleColor (UIColor.FromRGB (1, 122, 255), UIControlState.Normal);
|
|
||||||
|
|
||||||
AddSubviews (TextView, SendButton);
|
AddSubviews (TextView, SendButton);
|
||||||
|
|
||||||
|
@ -37,13 +48,11 @@ namespace Chat
|
||||||
NSLayoutFormatOptions.DirectionLeadingToTrailing,
|
NSLayoutFormatOptions.DirectionLeadingToTrailing,
|
||||||
"input", TextView
|
"input", TextView
|
||||||
);
|
);
|
||||||
var c3 = NSLayoutConstraint.FromVisualFormat ("V:[button]-|",
|
// We want Send button was centered when Toolbar has MinHeight (pin button in this state)
|
||||||
NSLayoutFormatOptions.DirectionLeadingToTrailing,
|
var c3 = NSLayoutConstraint.Create(SendButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, -ToolbarMinHeight / 2);
|
||||||
"button", SendButton
|
|
||||||
);
|
|
||||||
AddConstraints (c1);
|
AddConstraints (c1);
|
||||||
AddConstraints (c2);
|
AddConstraints (c2);
|
||||||
AddConstraints (c3);
|
AddConstraint (c3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -68,12 +68,7 @@ namespace Chat
|
||||||
chatInputView = new ChatInputView ();
|
chatInputView = new ChatInputView ();
|
||||||
|
|
||||||
SendButton.TouchUpInside += OnSendClicked;
|
SendButton.TouchUpInside += OnSendClicked;
|
||||||
|
|
||||||
TextView.Changed += OnTextChanged;
|
TextView.Changed += OnTextChanged;
|
||||||
TextView.Layer.BorderColor = UIColor.FromRGB (200, 200, 205).CGColor;
|
|
||||||
TextView.Layer.BorderWidth = (float)0.5;
|
|
||||||
TextView.Layer.CornerRadius = 5;
|
|
||||||
TextView.BackgroundColor = UIColor.FromWhiteAlpha (250, 1);
|
|
||||||
|
|
||||||
chatSrc = new ChatSource (messages, ShowMenu);
|
chatSrc = new ChatSource (messages, ShowMenu);
|
||||||
TableView.Source = chatSrc;
|
TableView.Source = chatSrc;
|
||||||
|
@ -137,8 +132,7 @@ namespace Chat
|
||||||
|
|
||||||
var offset = TableView.ContentOffset;
|
var offset = TableView.ContentOffset;
|
||||||
offset.Y += newOverlap - oldOverlap;
|
offset.Y += newOverlap - oldOverlap;
|
||||||
if(offset.Y < 0)
|
offset.Y = NMath.Max(offset.Y, 0);
|
||||||
offset.Y = 0;
|
|
||||||
TableView.ContentOffset = offset;
|
TableView.ContentOffset = offset;
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
|
@ -160,7 +154,6 @@ namespace Chat
|
||||||
return NMath.Min (TableView.ContentSize.Height - TableView.ContentOffset.Y, TableView.Frame.Height);
|
return NMath.Min (TableView.ContentSize.Height - TableView.ContentOffset.Y, TableView.Frame.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns changes in ContentInsetY and ContentOffsetY values
|
|
||||||
void UpdateTableInsets()
|
void UpdateTableInsets()
|
||||||
{
|
{
|
||||||
UIEdgeInsets oldInset = TableView.ContentInset;
|
UIEdgeInsets oldInset = TableView.ContentInset;
|
||||||
|
@ -206,7 +199,7 @@ namespace Chat
|
||||||
|
|
||||||
var indexPaths = new NSIndexPath[] { LastIndexPath };
|
var indexPaths = new NSIndexPath[] { LastIndexPath };
|
||||||
TableView.InsertRows(indexPaths, UITableViewRowAnimation.None);
|
TableView.InsertRows(indexPaths, UITableViewRowAnimation.None);
|
||||||
TableView.ScrollToRow (indexPaths [0], UITableViewScrollPosition.Bottom, true);
|
TableView.ScrollToRow (LastIndexPath, UITableViewScrollPosition.Bottom, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnTextChanged (object sender, EventArgs e)
|
void OnTextChanged (object sender, EventArgs e)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче