我正在尝试在Xamarin.iOS(在[[Xamarin.Forms中的编辑器)中的自定义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
的右侧),但是它一直粘在左侧。一种更清洁的方法可以实现这一点,将不胜感激问题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
(我尝试使用但没有用)与.LayoutMargins.TopAnchor
和.LayoutMargins.BottomAnchor
之间的区别的解释也将是可理解的。 :)