c# - 如何在 ios 项目的 Xamarin.Forms 编辑器中添加显示/隐藏密码图像。 (滚动和 UI 放置问题)

标签 c# ios xamarin xamarin.ios uitextview

我正在尝试在 Xamarin.iOS 中的自定义 UITextView 中放置一个按钮来实现显示/隐藏密码(Xamarin.Forms 中的编辑器) >)。然而,我在 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 滚动时,我无法阻止按钮滚动。每当发生滚动时,就会发生这种情况:

Scrolling-issue

基本上按钮会随着文本滚动,这并不理想。任何有关如何解决此问题的想法将不胜感激。

问题 2。我正在尝试获取允许用户选择是否希望密码显示在 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;

它给了我这个:

My solution

这是我想要的视觉结果,因为删除背景颜色后它看起来如下图所示:

My solution-2

但是,这个解决方案感觉相当hacky,在我希望使用AutoLayout anchor 属性应该有效的地方使用了很多常量。我尝试使用 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 将不胜感激。 :)

关于c# - 如何在 ios 项目的 Xamarin.Forms 编辑器中添加显示/隐藏密码图像。 (滚动和 UI 放置问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58274207/

相关文章:

c# - 请推荐一个好的 OpenStreetMap 组件

c# - 在 C# 中声明集合

iphone - UIScrollView如何自动包裹内容?

ios - 如何在 iOS 中实现 SSL Pinning?

ios - 当主线程使用量增加时,IOS音频线程CPU使用率下降

c# - WPF如何使StackPanel可滚动

c# - 如何使用 C# HttpClient PostAsync 显示上传进度

xamarin - 在 xamarin android 中为 facebook sdk 生成 key 哈希

c# - 如何在 android Xamarin 中动态填充表格布局?

c# - UWP C# - 如何使用 Windows.Data.Json 将 JsonObject 反序列化为一个类?