Forum Xamarin.iOS

Create contraints dinamically in a view with toolbar and picker.

JoseCarlosToledoMuozJoseCarlosToledoMuoz ESMember ✭✭
edited October 2013 in Xamarin.iOS

Hello,

I would like create a view with a toolbar and datepicker but I want add constraints to the view dinamically (autorotate includes). I have tried it but I don't get it.

        private void InitControls()
        {
            this.View.AutosizesSubviews = true;

            // add background dialog
            pBackground = new UIView(new RectangleF(this.View.Frame.X, this.View.Frame.Y, this.View.Frame.Width, this.View.Frame.Height));
            pBackground.BackgroundColor = UIColor.Blue;
            pBackground.AutoresizingMask = UIViewAutoresizing.All;
            pBackground.Alpha = 0.85f;

            pDialog = new UIView(new RectangleF(pBackground.Frame.X, pBackground.Frame.Y, pBackground.Frame.Width, pBackground.Frame.Height));
            pDialog.BackgroundColor = UIColor.Clear;   
            pDialog.AutoresizingMask = UIViewAutoresizing.All;

            // add toolbar
            pToolBar = new UIToolbar(new RectangleF(0, 0, this.View.Frame.Width, 50));
            pToolBar.BarStyle = UIBarStyle.Default;
            pToolBar.BackgroundColor = UIColor.Green;
            pToolBar.Translucent = true;

            pDialog.TranslatesAutoresizingMaskIntoConstraints = false;
            pToolBar.TranslatesAutoresizingMaskIntoConstraints = false;

            // Add the done button
            UIBarButtonItem doneButton = new UIBarButtonItem("Ok", UIBarButtonItemStyle.Done, null);
            doneButton.Clicked += (object sender, EventArgs e) => {
                Ok();
            };

            // Add the cancel button
            UIBarButtonItem backButton = new UIBarButtonItem("Back", UIBarButtonItemStyle.Done, null);
            backButton.Clicked += (object sender, EventArgs e) => {
                Cancel();
            };

            pToolBar.Items = new UIBarButtonItem[] { backButton, new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null, null), doneButton };

            pDialog.AddSubview(pToolBar);

            pPicker = new UIDatePicker(new RectangleF(0, 0, 200, 150));
            pPicker.Mode = UIDatePickerMode.Time; //Tipo;
            pPicker.Date = DateTime.Now; //Initial;
            pPicker.BackgroundColor = UIColor.White;
            pPicker.TranslatesAutoresizingMaskIntoConstraints = false;

            pPicker.ValueChanged += (sender, e) => 
            {
                //DatePickerToDateTime();
            };


            pDialog.AddSubview(pPicker);

            CreateConstraints ();

            pBackground.AddSubview(pDialog);
            this.View.AddSubview (pBackground);

        }

        private void CreateConstraints()
        {   

            var viewsDictionary = NSDictionary.FromObjectsAndKeys(new NSObject[] { pPicker, pToolBar}, new NSObject[] { new NSString("picker"), new NSString("toolbar")});
            pDialog.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|-" + this.View.Frame.Height * 2 / 3 + "-[toolbar][picker]-10-|", 0, new NSDictionary (), viewsDictionary));
            pDialog.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-10-[toolbar]-10-|", 0, new NSDictionary (), viewsDictionary));
            pDialog.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-10-[picker]-10-|", 0, new NSDictionary (), viewsDictionary));

        }
Sign In or Register to comment.