Swift Training Anchor Does Not Work

Asked 2 years ago, Updated 2 years ago, 29 views

trailingAnchor does not work at all in the code below.

As a minimum test, configuring two horizontal constraints: leadingAnchor and trailingAnchor with one button, or configuring only trailingAnchor, ignores the relationship with the right edge of the trailingAnchor and displays only to the left.

I think trailingAnchor is not working, but the width does not change when you rotate the screen, but the vertical direction has auto-layout and the vertical width changes as restricted.

Could you please let me know if there are any mistakes?
Also, I would appreciate it if you could tell me how to improve it.

Test Environment
·Xcode 8.3.2
·iOS 10.3.3 (iPadMini4 actual)

@IBOutlet var buttons: UIButton = [ ]

    ...omitted...

    // Display buttons
    func buttonIndicate(){

        for roop in 0...11 {
            // Position the button horizontally
            let button —UIButton=UIButton()
            // Text Displayed
            button.setTitle(String(roop), for:.normal)
            // Text Color
            button.setTitleColor (UIColor.black, for:.normal)
            // Background color
            button.backgroundColor=UIColor.green
            // Tag number
            button.tag=roop
            button.restorationIdentifier=String(roop)
            // To avoid conflicts with the restrictions to be added after this time
            button.translatesAutoresizingMaskIntoConstraints=false

            buttons.append (button)

        }

        let keyColum=11

        Add to //view to set constraints
        for roop in 0...11 {
            self.view.addSubview (buttons [roop])
            // X-direction constraints
            ifroop == 0 {
                // Relationship between the first button and the far left
                self.buttons [roop].leadingAnchor.constraint(equalTo:self.view.leadingAnchor, constant:1).isActive=true
            } else{
                // Relationship with previous buttons
                self.buttons [roop].leadingAnchor.constraint(equalTo:self.buttons [roop-1].trailingAnchor,constant:2).isActive=true
            }
            // Same width as button before width constraint
            ifroop!=0{
                self.buttons [roop].widthAnchor.constraint(equalTo:self.buttons [roop-1].widthAnchor).isActive=true
            }
            // The relationship between the rightmost button and the rightmost button is not working at all
            ifroop==keyColum{
                self.buttons [roop].trailingAnchor.constraint(equalTo:self.view.trailingAnchor, constant:-1)
            }
            // Y-direction constraints
            Relationship with //top
            self.buttons [roop].topAnchor.constraint(equalTo:self.view.topAnchor, constant:0).isActive=true
            // Relationship with Bottom
            self.buttons [roop].bottomAnchor.constraint(equalTo:self.view.bottomAnchor, constant:-10).isActive=true
        }

    }

swift ios

2022-09-29 22:01

1 Answers

It was a careless mistake.
isActive=true was missing.
I'm sorry.


2022-09-29 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.