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

问题描述 投票:0回答:1

我正在尝试在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滚动时,我无法阻止按钮滚动。每当滚动发生时,都会发生:Scrolling-issue

基本上,按钮与不理想的文本一起滚动。任何有关如何解决此问题的想法将不胜感激。

问题2

。我正在尝试获得一个按钮,该按钮使用户可以选择是否希望密码显示在UITextViewright侧。我已经使用下面的代码来做到这一点。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

这是我想要的

visual

结果,因为除去背景色后,它看起来像下面的图像:My solution-2

但是,此解决方案感觉很

hacky,在我希望使用AutoLayout锚定属性应该起作用的地方使用了许多常量。我尝试将TrailingAnchor属性与ConstraintEqualTo结合使用来实现此目的([this

粘在UITextView的右侧),但是它一直粘在左侧。一种更清洁的方法可以实现这一点,将不胜感激
c# ios xamarin xamarin.ios uitextview
1个回答
0
投票
有了灵感,来自@ 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(我尝试使用但没有用)与.LayoutMargins.TopAnchor.LayoutMargins.BottomAnchor之间的区别的解释也将是可理解的。 :)

© www.soinside.com 2019 - 2024. All rights reserved.