0

我想要一个圆形 UIButton,其大小将受其标题的限制。此按钮将放置在水平堆栈视图中,并带有前面的文本。我希望按钮的大小是固定的,而前面文本的大小仅受按钮旁边可用空间的限制。

这是我到目前为止的代码:

UIStackView textAndDoneButtonStackView = new UIStackView
{
    TranslatesAutoresizingMaskIntoConstraints = false,
    Axis = UILayoutConstraintAxis.Horizontal,
    Distribution = UIStackViewDistribution.Fill,
    Alignment = UIStackViewAlignment.Center,
    Spacing = 10
};

// Create the appropriate control for this habit                
UIView habitTitleView = CreateControlForHabit(habitTitle, kv.Value.workout, workoutName);
textAndDoneButtonStackView.AddArrangedSubview(habitTitleView);

// Add the done button
UIButton doneButton = CreateDoneButton(healthy_green);
habitIdDoneButtonId[kv.Value.id] = (int)doneButton.Tag;

textAndDoneButtonStackView.AddArrangedSubview(doneButton);

CreateControlForHabit(...)
{
    UILabel uIHabitTitle = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false };
    uIHabitTitle.Text = habitTitle;
    return uIHabitTitle;
}

然后

private static UIButton CreateDoneButton(UIColor healthy_green)
{
    // Add the done button

    var doneButton = UIButton.FromType(UIButtonType.RoundedRect);
    doneButton.TranslatesAutoresizingMaskIntoConstraints = false;

    doneButton.SetTitle(Utilities.GetLocalizedString("habit_done_button_text"), UIControlState.Normal);
    doneButton.SetTitleColor(UIColor.White, UIControlState.Normal);
    doneButton.BackgroundColor = healthy_green; // Healthy Green
    doneButton.Layer.CornerRadius = 5f;
    doneButton.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
    doneButton.ContentEdgeInsets = new UIEdgeInsets(top: 0, left: 10f, bottom: 0, right: 10f);

    doneButton.Tag = doneButton.GenerateViewTag();

    // View is hidden by default
    doneButton.Hidden = true;

    return doneButton;
}

我的问题是我想要一个圆形按钮,并且我不能将 UIButton.FromType() 的结果转换为将覆盖 IntrinsicContentSize 的自定义类。

此处有哪些选项可用于修复此圆形按钮的大小?

4

0 回答 0