anyone can help me how to work with percentage and constraints

Hello,

I want to design a layout that work on all devices indepently with the size of the device so i should work with percentages and constraints
anyone have worked with constraints by code i doesn't understant them very well

thanks in advance

Answers

  • JF.0444JF.0444 USMember ✭✭✭

    This is a view controller which adds a green view to the top using constraints

    public partial class ViewController : UIViewController
    {
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
    
            var newView = new UIView();
            newView.BackgroundColor = UIColor.Green;
    
            // this is required or else custom constraints will not work
            newView.TranslatesAutoresizingMaskIntoConstraints = false;
    
            // must add view to superview before applying constraints
            View.AddSubview(newView);
    
            // anchor view to left of superview
            this.View.AddConstraint(
                NSLayoutConstraint.Create(
                    newView, 
                    NSLayoutAttribute.Left, 
                    NSLayoutRelation.Equal,
                    this.View, 
                    NSLayoutAttribute.Left,
                    1, 
                    10));
    
            // anchor view to right of superview
            this.View.AddConstraint(
                NSLayoutConstraint.Create(
                    this.View,
                    NSLayoutAttribute.Right,
                    NSLayoutRelation.Equal, 
                    newView, 
                    NSLayoutAttribute.Right, 
                    1, 
                    10));
    
            // anchor view to top of superview
            this.View.AddConstraint(
                NSLayoutConstraint.Create(
                    newView,
                    NSLayoutAttribute.Top, 
                    NSLayoutRelation.Equal, 
                    this.View,
                    NSLayoutAttribute.Top,
                    1, 
                    10));
    
            // height constraint
            newView.AddConstraint(
                NSLayoutConstraint.Create(
                    newView,
                    NSLayoutAttribute.Height,
                    NSLayoutRelation.Equal,
                    1, 
                    100));
        }
    
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
    

    You can also use different constraints for different size classes right on the storyboard, at the bottom of this link there is some helpful info on that

    https://developer.xamarin.com/guides/ios/platform_features/introduction_to_Unified_Storyboards/

Sign In or Register to comment.