Jonathan Bick
Jonathan Bick

Reputation: 939

UIButton not showing in UIView Xamarin

I followed one of the Xamarin recipes (found here: http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message), but instead of using a activity spinner I wanted to add a UIButton instead. I subbed out the activity spinner and am left with the following UIView class, however when I load the view the button is not loading/visible, however the label I've added is visible. Any Ideas?

Source Code:

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(); }
        );
    }
};

Thanks

Upvotes: 0

Views: 3720

Answers (2)

Ben Bishop
Ben Bishop

Reputation: 1414

Your problem is in this part of your code:

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

You're setting the x, y, width, and height of the Button's Frame before it's been set.

Changing the code to this fixes the problem:

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

Upvotes: 1

Jason
Jason

Reputation: 89082

You have specified a frame for your UILabel, but not your UIButton. You have to specify a frame to determine where in the view the element will appear.

Upvotes: 0

Related Questions