0

How do I render a UIButton in Xamarin.iOS? See the current Code for the full list.

This is the code I'm using to create and add the button to the Xamarin.Forms.Platform.iOS.CellTableViewCell cell. I cannot get the button to display anything. With the use of a Foundation.NSMutableAttributedString, it shows a cut-off section of text in the top left corner, regardless of anything I try (alignments, insets, bounds, various constraints, etc). I'm currently trying things from Xamarin.Forms.Platform.iOS.Renderers.ButtonRenderer, but still can't get anything to display at all, no text, no button, or its outline.

If you could fork the repo and fix it or post the solution here, I would be very grateful.

protected override void SetUpContentView()
{
    var insets = new UIEdgeInsets(SVConstants.Cell.PADDING.Top.ToNFloat(), SVConstants.Cell.PADDING.Left.ToNFloat(), SVConstants.Cell.PADDING.Bottom.ToNFloat(), SVConstants.Cell.PADDING.Right.ToNFloat());
    _Button = new UIButton(UIButtonType.RoundedRect)
              {
                  AutoresizingMask = UIViewAutoresizing.All,
                  HorizontalAlignment = UIControlContentHorizontalAlignment.Center,
                  VerticalAlignment = UIControlContentVerticalAlignment.Center,
                  ContentEdgeInsets = insets,
                  // TitleEdgeInsets = insets
              };

    DefaultFontSize = _Button.TitleLabel.ContentScaleFactor;
    DefaultTextColor = _Button.TitleLabel.TextColor;

    _Recognizer = new UILongPressGestureRecognizer(RunLong);
    _Button.TouchUpInside += OnClick;          // https://stackoverflow.com/a/51593238/9530917
    _Button.AddGestureRecognizer(_Recognizer); // https://stackoverflow.com/a/6179591/9530917


    ContentView.AddSubview(_Button);
    _Button.CenterXAnchor.ConstraintEqualTo(ContentView.CenterXAnchor).Active = true;
    _Button.CenterYAnchor.ConstraintEqualTo(ContentView.CenterYAnchor).Active = true;

    _Button.WidthAnchor.ConstraintEqualTo(ContentView.WidthAnchor).Active = true;
    _Button.HeightAnchor.ConstraintEqualTo(ContentView.HeightAnchor).Active = true;

    UpdateConstraintsIfNeeded();
    LayoutIfNeeded();
}
Jakar510
  • 181
  • 3
  • 21
  • Also the GitHub issue: https://github.com/xamarin/Xamarin.Forms/issues/14022 – Jakar510 Mar 17 '21 at 03:58
  • Could you show me a screenshot of the effect you want ? – Leo Zhu Mar 17 '21 at 07:41
  • What i'd like, honestly for both android and ios, is https://devblogs.microsoft.com/xamarin/beautiful-material-design-android-ios/ – Jakar510 Mar 17 '21 at 14:50
  • @LeoZhu-MSFT But anything that shows up at this point is ok. – Jakar510 Mar 22 '21 at 03:17
  • Found out that you can't subclass it. Any button added to the view must be native (UIButton) or custom rendered, such as Xamarin.Forms.Platform.iOS.ButtonRenderer; It doesn't show up otherwise. – Jakar510 Apr 12 '21 at 14:40

1 Answers1

0

Found out that you can't subclass it. Any button added to the view must be native (UIButton) or custom rendered, such as Xamarin.Forms.Platform.iOS.ButtonRenderer; It doesn't show up otherwise.

Jakar510
  • 181
  • 3
  • 21