我正在尝试在Xamarin.iOS中的自定义UITextView
中放置一个按钮来实现显示/隐藏密码。但是,我在UI设计方面面临两(2)个问题。
我已经使用此代码来设置显示/隐藏密码按钮,并且效果很好:
UITextView vUpdatedEntry = (UITextView)Control;
var buttonRect = UIButton.FromType(UIButtonType.Custom);
buttonRect.SetImage(UIImage.FromBundle("show_black_24"), UIControlState.Normal);
buttonRect.TouchUpInside += (object sender, EventArgs e1) => {
if (vUpdatedEntry.SecureTextEntry)
{
vUpdatedEntry.SecureTextEntry = false;
buttonRect.SetImage(UIImage.FromBundle("hide_black_24"), UIControlState.Normal);
}
else
{
vUpdatedEntry.SecureTextEntry = true;
buttonRect.SetImage(UIImage.FromBundle("show_black_24"), UIControlState.Normal);
}
};
问题1。当UITextView
滚动时,我无法阻止按钮滚动。每当滚动发生时,都会发生:
基本上,按钮与不理想的文本一起滚动。任何有关如何解决此问题的想法将不胜感激。
问题2。我正在尝试获得一个按钮,该按钮允许用户选择是否希望密码显示在UITextView
的right侧。我已经使用下面的代码来做到这一点。
buttonRect.Frame = new CoreGraphics.CGRect(10.0f, 0.0f, 15.0f, 15.0f);
buttonRect.ContentMode = UIViewContentMode.ScaleToFill;
buttonRect.BackgroundColor = UIColor.Orange;
UIView paddingViewRight = new UIView(new System.Drawing.RectangleF(0.0f, 0.0f, 30.0f, 18.0f));
paddingViewRight.BackgroundColor = UIColor.Purple;
paddingViewRight.AddSubview(buttonRect);
buttonRect.TranslatesAutoresizingMaskIntoConstraints = false;
buttonRect.CenterYAnchor.ConstraintEqualTo(paddingViewRight.CenterYAnchor).Active = true;
vUpdatedEntry.TextContainerInset = new UIEdgeInsets(8.0f, 0.0f, 8.0f, paddingViewRight.Frame.Width+5.0f);
vUpdatedEntry.AddSubview(paddingViewRight);
paddingViewRight.TranslatesAutoresizingMaskIntoConstraints = false;
paddingViewRight.TrailingAnchor.ConstraintEqualTo(vUpdatedEntry.LayoutMarginsGuide.TrailingAnchor, 9.0f).Active = true;
paddingViewRight.HeightAnchor.ConstraintEqualTo(vUpdatedEntry.HeightAnchor).Active = true;
paddingViewRight.WidthAnchor.ConstraintEqualTo(buttonRect.WidthAnchor,1.0f, 0.0f).Active = true;
Control.Layer.CornerRadius = 4;
Control.Layer.BorderColor = new CoreGraphics.CGColor(255, 255, 255);
Control.Layer.MasksToBounds = true;
vUpdatedEntry.TextAlignment = UITextAlignment.Left;
它给了我这个:
这是我想要的visual结果,因为除去背景色后,它看起来像下面的图像:
但是,此解决方案感觉很hacky,在我希望使用AutoLayout
锚定属性应该起作用的地方使用了许多常量。我尝试将TrailingAnchor
属性与ConstraintEqualTo
结合使用以实现此目的([this对UITextView
的右侧具有粘性),但是它一直对左侧保持粘性。一种更清洁的方法可以实现这一点,将不胜感激
有了灵感,来自@ JackHua-MSFT(对该问题发表评论的人),我能够弄清楚如何解决问题1,这是问题中最紧迫的问题,以下是我的代码:]。 >
buttonRect.ContentMode = UIViewContentMode.ScaleToFill; UIView paddingViewRight = new UIView(new System.Drawing.RectangleF(0.0f, 0.0f, 30.0f, 18.0f)); paddingViewRight.AddSubview(buttonRect); buttonRect.TranslatesAutoresizingMaskIntoConstraints = false; buttonRect.CenterYAnchor.ConstraintEqualTo(paddingViewRight.CenterYAnchor).Active = true; vUpdatedEntry.AddSubview(paddingViewRight); paddingViewRight.TranslatesAutoresizingMaskIntoConstraints = false; paddingViewRight.TrailingAnchor.ConstraintEqualTo(vUpdatedEntry.LayoutMarginsGuide.TrailingAnchor, 8.0f).Active = true; paddingViewRight.HeightAnchor.ConstraintEqualTo(vUpdatedEntry.HeightAnchor).Active = true; paddingViewRight.BottomAnchor.ConstraintEqualTo(vUpdatedEntry.LayoutMarginsGuide.BottomAnchor).Active = true; paddingViewRight.TopAnchor.ConstraintEqualTo(vUpdatedEntry.LayoutMarginsGuide.TopAnchor, -8.0f).Active = true; paddingViewRight.WidthAnchor.ConstraintEqualTo(buttonRect.WidthAnchor, 1.0f, 3.0f).Active = true; vUpdatedEntry.TextContainerInset = new UIEdgeInsets(8.0f, 0.0f, 8.0f, paddingViewRight.Frame.Width + 5.0f); Control.Layer.CornerRadius = 4; Control.Layer.BorderColor = new CoreGraphics.CGColor(255, 255, 255); Control.Layer.MasksToBounds = true; vUpdatedEntry.TextAlignment = UITextAlignment.Left;
然而,对于更清洁的解决方案的任何建议(特别是我不需要在LayoutMarginsGuide中使用常量float值的地方]] >>。另外,对使用
.TopAnchor
和.BottomAnchor
(我尝试使用但不起作用)之间的区别的解释也将得到赞赏。 :)