【问题标题】:UIButton not showing in UIView XamarinUIButton 未在 UIView Xamarin 中显示
【发布时间】:2013-08-20 12:34:38
【问题描述】:

我遵循了 Xamarin 食谱之一(可在此处找到:http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message),但我不想使用活动微调器,而是想添加一个 UIButton。我替换了活动微调器并留下了以下 UIView 类,但是当我加载视图时,按钮没有加载/可见,但是我添加的标签是可见的。有什么想法吗?

源代码:

public class testOverlay : UIView {

    UIButton buttonRect;
    UILabel testLabel;

    public testOverlay (RectangleF frame) : base (frame)
    {
        // configurable bits
        BackgroundColor = UIColor.Black;
        Alpha = 0.75f;
        AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;

        float labelHeight = 22;
        float labelWidth = Frame.Width - 20;

        // derive the center x and y
        float centerX = Frame.Width / 2;
        float centerY = Frame.Height / 2;

        // create the activity spinner, center it horizontall and put it 5     points above center x
        buttonRect = UIButton.FromType(UIButtonType.RoundedRect);
        buttonRect.SetTitle ("Confirm", UIControlState.Normal);
                    buttonRect.Frame = new RectangleF (
            centerX - (buttonRect.Frame.Width / 2) ,
            centerY - buttonRect.Frame.Height - 20 ,
            buttonRect.Frame.Width ,
            buttonRect.Frame.Height);
        buttonRect.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        AddSubview (buttonRect);

        // create and configure the "Loading Data" label
        testLabel = new UILabel(new RectangleF (
            centerX - (labelWidth / 2),
            centerY + 20 ,
            labelWidth ,
            labelHeight
            ));
        testLabel.BackgroundColor = UIColor.Clear;
        testLabel.TextColor = UIColor.White;
        testLabel.Text = "Loading...";
        testLabel.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        AddSubview (testLabel);
    }

    /// <summary>
    /// Fades out the control and then removes it from the super view
    /// </summary>
    public void Hide ()
    {
        UIView.Animate (
            0.5, // duration
            () => { Alpha = 0; },
        () => { RemoveFromSuperview(); }
        );
    }
};

谢谢

【问题讨论】:

    标签: c# xamarin


    【解决方案1】:

    您的问题出在这部分代码中:

    buttonRect.Frame = new RectangleF (
        centerX - (buttonRect.Frame.Width / 2) ,
        centerY - buttonRect.Frame.Height - 20 ,
        buttonRect.Frame.Width ,
        buttonRect.Frame.Height);
    

    您正在设置按钮框架的 x、y、宽度和高度,然后才设置它。

    将代码改成这样可以解决问题:

    var buttonWidth = 100;
    var buttonHeight = 50;
    buttonRect.Frame = new RectangleF (
        centerX - buttonWidth / 2 ,
        centerY - buttonHeight - 20 ,
        buttonWidth ,
        buttonHeight);
    

    【讨论】:

    • 你是个天才,谢谢。我将两部分代码拼凑在一起,并没有意识到宽度和高度是在正在创建的框架内设置的。
    【解决方案2】:

    您已经为您的 UILabel 指定了一个框架,但没有为您的 UIButton。您必须指定一个框架来确定元素将出现在视图中的哪个位置。

    【讨论】:

    • 感谢您的快速响应,我已经更新了上面的代码,但仍然没有显示按钮。有什么建议?我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多