UIButton没有出现在UIView Xamarin中

时间:2013-08-20 12:34:39

标签: c# xamarin

我遵循了一个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(); }
        );
    }
};

由于

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您为UILabel指定了一个框架,但没有为您的UIButton指定框架。您必须指定一个框架以确定元素在视图中的显示位置。