如何在 ios 项目的 Xamarin.Forms 编辑器中添加 show/hide 密码图像。 (滚动和 UI 放置问题)

How to add show/hide password image in a Xamarin.Forms Editor for ios project. (scrolling and UI placement issues)

我正在尝试在 Xamarin.iOS 中的自定义 UITextView 中放置一个按钮来实现 show/hide 密码([=50 中的编辑器=]Xamarin.Forms)。但是,我在 UI 设计方面面临两 (2) 个问题。

我已经使用了这段用于 show/hide 密码按钮设置的代码,它运行良好:

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 期。我正在尝试获取允许用户 select 是否希望密码在 UITextView 右侧 一侧可见的按钮.我已经使用下面的代码来做到这一点。

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;

它给了我这个:

这是我想要的视觉结果,因为移除背景颜色后看起来像下图:

然而,这个解决方案感觉相当 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 中使用常量浮点值的建议),我们将不胜感激。还可以解释使用 .TopAnchor.BottomAnchor(我尝试使用但没有用)与 .LayoutMargins.TopAnchor.LayoutMargins.BottomAnchor 之间的区别。 :)