2013-08-20 2 views
0

Я следовал за одним из рецептов Xamarin (найдено здесь: http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message), но вместо использования spinner для работы я хотел вместо этого добавить UIButton. Я подчинил действие spinner и оставил следующий класс UIView, однако, когда я загружаю представление, кнопка не загружается/отображается, однако ярлык, который я добавил, виден. Есть идеи?UIButton не отображается в UIView Xamarin

Исходный код:

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

Благодаря

ответ

1

Ваша проблема в этой части кода:

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

Вы установки х, у, ширину и высоту рамы в Баттона, прежде чем он был установлен.

Изменение кода на это исправляет проблему:

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

Ты гений, спасибо. Я собрал две части кода и не понял, что ширина и высота устанавливаются внутри создаваемого фрейма. –

0

Вы определили рамки для вашего UILabel, но не ваш UIButton. Вы должны указать кадр, чтобы определить, где в представлении элемент появится.

+0

Спасибо за быстрый ответ, я обновил свой код выше, но его все еще не показывает кнопку. Какие-либо предложения? Я что-то пропустил? –

Смежные вопросы