简体   繁体   中英

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

I am trying to place a button to implement show/hide password in a custom UITextView in Xamarin.iOS (Editor in Xamarin.Forms ). However I am facing two (2) issues with the UI design.

I have used this code used for the show/hide password button setup, and it works perfectly:

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);
    }
};

Issue 1 . I am unable to prevent the button from scrolling away when the UITextView scrolls. Whenever scrolling occurs, this happens:

滚动问题

Basically the button scrolls along with the text which is not ideal. Any ideas on how to fix this would be much appreciated.

Issue 2 . I am trying to get the button which allows the user to select whether they want the password to be visible or not to be on the right side of the UITextView . I have used the code below to do this.

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;

and it gives me this:

我的解决方案

This is my desired visual outcome, as it looks like the image below when the background colors are removed:

我的解决方案-2

However, this solution feels rather hacky with lots of constants used in places where I would expect that using an AutoLayout anchor property should have worked. I tried using the TrailingAnchor property with ConstraintEqualTo to achieve this ( this being stickiness to the right side of UITextView ), but it kept sticking to the left-side instead. A cleaner approach to achieving this would be much appreciated

With Inspiration, from @JackHua-MSFT (the poster of comment on the question) I was able to figure out how to fix Issue 1 which was the more pressing issue in the question, My code below:.

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;

However any suggestions for a cleaner solution (specifically one where I don't need to use constant float values with the LayoutMarginsGuide) would be appreciated. Also an explanation on the difference between using .TopAnchor and .BottomAnchor (which I tried using but didn't work) vs .LayoutMargins.TopAnchor and .LayoutMargins.BottomAnchor would be appreciated. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM