【发布时间】:2023-04-09 03:54:01
【问题描述】:
如何在 Xamarin.iOS 中呈现 UIButton?完整列表请参见当前的Code。
这是我用来创建按钮并将其添加到Xamarin.Forms.Platform.iOS.CellTableViewCell 单元格的代码。我无法让按钮显示任何内容。
使用Foundation.NSMutableAttributedString,它会在左上角显示一个截断的文本部分,无论我尝试什么(对齐、插入、边界、各种约束等)。我目前正在尝试来自 Xamarin.Forms.Platform.iOS.Renderers.ButtonRenderer 的东西,但仍然无法显示任何内容,没有文本、没有按钮或其轮廓。
如果您可以分叉回购并修复它或在此处发布解决方案,我将非常感激。
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();
}
【问题讨论】:
-
还有 GitHub 问题:github.com/xamarin/Xamarin.Forms/issues/14022
-
可以给我截图你想要的效果吗?
-
老实说,对于 android 和 ios,我想要的是 devblogs.microsoft.com/xamarin/…
-
@LeoZhu-MSFT 但是此时显示的任何内容都可以。
-
发现你不能继承它。添加到视图中的任何按钮都必须是本机 (UIButton) 或自定义呈现的,例如 Xamarin.Forms.Platform.iOS.ButtonRenderer;否则不会显示。
标签: ios xamarin xamarin.ios uibutton renderer