Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ios项目的Xamarin.Forms编辑器中添加显示/隐藏密码图像。(滚动和UI放置问题)_C#_Ios_Xamarin_Xamarin.ios_Uitextview - Fatal编程技术网

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

C# 如何在ios项目的Xamarin.Forms编辑器中添加显示/隐藏密码图像。(滚动和UI放置问题),c#,ios,xamarin,xamarin.ios,uitextview,C#,Ios,Xamarin,Xamarin.ios,Uitextview,我试图在Xamarin.iOS(编辑器位于Xamarin.Forms)中的自定义UITextView中放置一个按钮来实现显示/隐藏密码。然而,我在UI设计方面面临两(2)个问题 我将此代码用于“显示/隐藏密码”按钮设置,它可以完美地工作: UITextView vUpdatedEntry = (UITextView)Control; var buttonRect = UIButton.FromType(UIButtonType.Custom); buttonRect.SetImage(UIIma

我试图在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
右侧。我使用了下面的代码来实现这一点

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;
它给了我这个:

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


然而,这个解决方案感觉相当粗糙,在我认为使用
AutoLayout
anchor属性应该有效的地方使用了大量常量。我尝试将
TrailingAnchor
属性与
ConstraintEqualTo
一起使用,以实现这一点(粘贴到
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
之间的区别

对于问题1,我建议您不要为buttonRect提供框架,而是使用autolayout来布局buttonRect。对于问题2,我将使用与您相同的方法来实现它。@JackHua MSFT您好,谢谢您的输入。你不一定给我答案,但你确实让我朝着正确的方向思考,我能够使用
paddingViewRight.BottomAnchor.ConstraintEqualTo(vUpdateEntry.LayoutMarginsGuide.BottomAnchor,6.0f)解决问题1
@JackHua MSFT我还希望您能解释一下为什么我在使用
锚定的约束时必须使用
.LayoutMarginsGuide
。我可以使用
paddingViewRight.LeadingAnchor.ConstraintEqualTo(vupdatentry.LeadingAnchor)
paddingViewRight
的左边缘粘到
vupdatentry
的左边缘。但是,当尝试对
牵引锚执行相同操作时,这不起作用。我必须使用
.LayoutMarginsGuide
,以常数作为第二个参数,以获得我想要的结果,我希望您能解释为什么会这样,或者建议一些资源来解释原因。如果我理解正确,请设置equal TopAnchor,BottomAnchor具有设置等高的相同结果。@JackHua MSFT这是我所期望的,但是删除高度锚约束会导致
paddingViewRight
UITextView
底部之间的空间过大