खोज…


IOS 9+ लेआउट एंकर के साथ बाधाओं को जोड़ना

9.0
// Since the anchor system simply returns constraints, you still need to add them somewhere.
View.AddConstraints(
    new[] {
        someLabel.TopAnchor.ConstraintEqualTo(TopLayoutGuide.GetBottomAnchor()),
        anotherLabel.TopAnchor.ConstraintEqualTo(someLabel.BottomAnchor, 6),
        oneMoreLabel.TopAnchor.ConstraintEqualTo(anotherLabel.BottomAnchor, 6),
        oneMoreLabel.BottomAnchor.ConstraintGreaterThanOrEqualTo(BottomLayoutGuide.GetTopAnchor(), -10),
    }
);

दृश्य प्रारूप भाषा (VFL) का उपयोग कर बाधाओं को जोड़ना

// Using Visual Format Language requires a special look-up dictionary of names<->views.
var views = new NSDictionary(
    nameof(someLabel), someLabel,
    nameof(anotherLabel), anotherLabel,
    nameof(oneMoreLabel), oneMoreLabel
);
// It can also take a look-up dictionary for metrics (such as size values).
// Since we are hard-coding those values in this example, we can give it a `null` or empty dictionary.
var metrics = (NSDictionary)null;

// Add the vertical constraints to stack everything together.
// `V:` = vertical
// `|…|` = constrain to super view (`View` for this example)
// `-10-` = connection with a gap of 10 pixels (could also be a named parameter from the metrics dictionary)
// `-[viewName]-` = connection with a control by name looked up in views dictionary (using C# 6 `nameof` for refactoring support)
var verticalConstraints = NSLayoutConstraint.FromVisualFormat(
    $"V:|-20-[{nameof(someLabel)}]-6-[{nameof(anotherLabel)}]-6-[{nameof(oneMoreLabel)}]->=10-|",
    NSLayoutFormatOptions.AlignAllCenterX,
    metrics,
    views
);
View.AddConstraints(verticalConstraints);

आपको कुछ बाधाएं मिल सकती हैं, जैसे कि पहलू अनुपात , दृश्य प्रारूप भाषा (VFL) सिंटैक्स में व्यक्त नहीं किया जा सकता है और इसे उचित तरीकों को सीधे कॉल करना चाहिए।

Cirrious.FluentLayout का उपयोग करना

NuGet का उपयोग करना

Install-Package Cirrious.FluentLayout

GitHub पेज पर स्टार्टर उदाहरण के आधार पर एक विस्तारित उदाहरण, एक सरल पहला नाम, अंतिम नाम लेबल और फ़ील्ड सभी एक के ऊपर एक स्टैक किए गए:

public override void ViewDidLoad()
{
    //create our labels and fields
    var firstNameLabel = new UILabel();
    var lastNameLabel = new UILabel();
    var firstNameField = new UITextField();
    var lastNameField = new UITextField();

    //add them to the View
    View.AddSubviews(firstNameLabel, lastNameLabel, firstNameField, lastNameField);
    
    //create constants that we can tweak if we do not like the final layout
    const int vSmallMargin = 5;
    const int vMargin = 20;
    const int hMargin = 10;

    //add our constraints
    View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();        
    View.AddConstraints(
        firstNameLabel.WithSameTop(View).Plus(vMargin),
        firstNameLabel.AtLeftOf(View).Plus(hMargin),
        firstNameLabel.WithSameWidthOf(View),

        firstNameField.WithSameWidth(firstNameLabel),
        firstNameField.WithSameLeft(firstNameLabel),
        firstNameField.Below(firstNameLabel).Plus(vSmallMargin),

        lastNameLabel.Below(firstNameField).Plus(vMargin),
        lastNameLabel.WithSameLeft(firstNameField),
        lastNameLabel.WithSameWidth(firstNameField),

        lastNameField.Below(lastNameLabel).Plus(vSmallMargin),
        lastNameField.WithSameWidth(lastNameLabel),
        lastNameField.WithSameLeft(lastNameLabel));
}

चिनाई के साथ बाधाओं को जोड़ना

चिनाई उद्देश्य-सी के लिए एक पुस्तकालय है, लेकिन xamarin ने इसके लिए एक बंधन बनाया है और इसे nuget पैकेज https://www.nuget.org/packages/Masonry/ के रूप में बनाया है।

Nuget इंस्टॉल करें

Install-Package Masonry

यह केंद्र युक्त दृश्य के केंद्र बिंदु के नीचे एक बटन 100 अंक का होता है और 200 और 400 बिंदुओं के बीच चौड़ाई निर्धारित करता है

this.loginBtn.MakeConstraints(make =>
{
    make.Width.GreaterThanOrEqualTo(new NSNumber(200));
    make.Width.LessThanOrEqualTo(new NSNumber(400));
    make.Center.EqualTo(this.View).CenterOffset(new CGPoint(0, 100));
});

यह युक्त दृश्य के केंद्र बिंदु से 100 अंक ऊपर एक स्केल की गई छवि को सेट करता है और फिर 0.5 के म्यूलिपियर के साथ युक्त दृश्य की चौड़ाई को चौड़ाई निर्धारित करता है जिसका अर्थ चौड़ाई का 50% है। यह तब पहलू अनुपात से कई गुना ऊँचाई निर्धारित करता है जो छवि को स्केल करता है लेकिन इसके सही पहलू अनुपात को बनाए रखता है

this.logo.MakeConstraints(make =>
{
    make.Center.EqualTo(this.View).CenterOffset(new CGPoint(0, -100));
    make.Width.EqualTo(this.View).MultipliedBy(0.5f);
    make.Height.EqualTo(this.logo.Width()).MultipliedBy(0.71f);
});


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow