2016-06-08 9 views
0

Ich brauche 5 Tasten Layout wie in diesem Bild: figure 1proportionale Größenanpassung von Schaltflächen in UIStackView

Ich brauche sie auf verschiedenen Geräten proportional zu ändern.

Ich habe diesen Code in einer Methode, die eine Ansicht mit 5 Tasten erstellt.

UIStackView *stackView = [[UIStackView alloc]init]; 

// Add buttons to stackView (buttons are initialised) 
[stackView addArrangedSubview:_button1]; 
[stackView addArrangedSubview:_button2]; 
[stackView addArrangedSubview:_button3]; 
[stackView addArrangedSubview:_button4]; 
[stackView addArrangedSubview:_button5]; 

stackView.translatesAutoresizingMaskIntoConstraints = false; 
[self.view addSubview:stackView]; 

stackView.axis = UILayoutConstraintAxisVertical; 
stackView.distribution = UIStackViewDistributionFillEqually; 
stackView.spacing = 5; 

//Layout Constraints for stackView 
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true; 
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true; 

// 80% of screen height should be used for buttons 
CGFloat screenHeightAvailableForButtons = 80/100; 
// Calculate multiplier for height of the buttons (5 buttons) 
CGFloat buttonMultiplier = screenHeightAvailableForButtons /5; 
// 25% of screen width should be used for buttons 
CGFloat screenWidthMultiplier = 25/100; 

[_button1.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier].active = true; 
[_button1.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true; 

[_button2.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true; 
[_button2.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true; 

[_button3.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true; 
[_button3.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true; 

[_button4.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true; 
[_button4.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true; 

[_button5.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true; 
[_button5.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true; 

Dieser Code gibt eine leere Ansicht zurück. Benutze ich die Methoden falsch?
Ich kann nicht herausfinden, was ich falsch mache. Ich brauche das programmatisch in Ziel c.

dank

+0

Bitte näher auf die „Ich brauche sie proportional auf verschiedenen Geräten zu ändern.“. Wie "wenn die Höhe der Stapelansicht xxx ist, sind die Schaltflächen xx, dann sollte die Höhe der Schaltfläche xxx sein" und ähnlich wie die Breite. – BangOperator

+0

@BangOperator Ich brauche es aussehen wie es ist in dem bereitgestellten Bild 'auf allen Geräten' und ich muss es programmatisch nicht in IB tun. Also, im Querformat, stackView.height = screen.height minus 20 Punkte von oben und unten, button.height = 1/5 von stackView mit 5 Punkt Abstand zwischen den einzelnen Schaltflächen. Und stackView.width = 1/4 der Bildschirmbreite und 20 Punkte vom linken Rand entfernt, button.width = stackView.width. Hoffe das macht Sinn – sharp

Antwort

1

Für Ihren Fall brauchen Sie nicht für Schaltflächen, um die Einschränkungen in der Stapelansicht einzustellen. Die Stapelansicht verarbeitet dies automatisch.

Versuchen Sie, diese

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIStackView *stackView = [[UIStackView alloc]initWithFrame:self.view.bounds]; 
    stackView.backgroundColor = [UIColor darkGrayColor]; 
    _button1 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [_button1 setTitle:@"Button 1" forState:UIControlStateNormal]; 
    _button1.translatesAutoresizingMaskIntoConstraints = NO; 
    _button1.backgroundColor = [UIColor lightGrayColor]; 

    _button2 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [_button2 setTitle:@"Button 2" forState:UIControlStateNormal]; 
    _button2.translatesAutoresizingMaskIntoConstraints = NO; 
    _button2.backgroundColor = [UIColor lightGrayColor]; 

    _button3 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [_button3 setTitle:@"Button 3" forState:UIControlStateNormal]; 
    _button3.translatesAutoresizingMaskIntoConstraints = NO; 
    _button3.backgroundColor = [UIColor lightGrayColor]; 


    _button4 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [_button4 setTitle:@"Button 4" forState:UIControlStateNormal]; 
    _button4.translatesAutoresizingMaskIntoConstraints = NO; 
    _button4.backgroundColor = [UIColor lightGrayColor]; 

    _button5 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [_button5 setTitle:@"Button 5" forState:UIControlStateNormal]; 
    _button5.translatesAutoresizingMaskIntoConstraints = NO; 
    _button5.backgroundColor = [UIColor lightGrayColor]; 

    // Add buttons to stackView (buttons are initialised) 
    [stackView addArrangedSubview:_button1]; 
    [stackView addArrangedSubview:_button2]; 
    [stackView addArrangedSubview:_button3]; 
    [stackView addArrangedSubview:_button4]; 
    [stackView addArrangedSubview:_button5]; 

    stackView.translatesAutoresizingMaskIntoConstraints = false; 
    [self.view addSubview:stackView]; 

    stackView.axis = UILayoutConstraintAxisVertical; 
    stackView.distribution = UIStackViewDistributionFillEqually; 
    stackView.alignment = UIStackViewAlignmentFill; 
    stackView.spacing = 5; 

    //Layout Constraints for stackView 
    [stackView.layoutMarginsGuide.topAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.topAnchor].active = true; 
    [stackView.layoutMarginsGuide.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor].active = true; 
    // 25% of screen width should be used for buttons 
    [stackView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:25.0/100.0].active = true; 
    [stackView.layoutMarginsGuide.centerXAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.centerXAnchor].active = true; 

} 
+0

Danke, mein Herr. Ich habe gelernt, dass ich auch 'constraintEqualToAnchor: constant:' verwenden kann, um der Bedingung einen 'konstanten' Wert hinzuzufügen, um die stackView auf dem Bildschirm zu positionieren, wo immer ich will! Tolles Zeug;) – sharp

+0

@DannyHuseyin wenn das beantwortet ist, fragen Sie es als beantwortet. – BangOperator

+0

Fertig. Ich werde sicherstellen, dass ich upvote, wenn mein rep erlaubt – sharp

Verwandte Themen