[SWIFT] Error when using SnapKit

The error that appeared

Fatal error: Expected superview but found nil when attempting make constraint equalToSuperview.

Cause and solution

It was because I had addSubview (recordingButton) after equalToSuperView. If you don't fix the parent view first, you can't decide relative to the parent, so it's natural.

private func setupRecordingButton() {
        recordingButton.setTitle("Record", for: .normal)
        recordingButton.backgroundColor = .yellow
        
        recordingButton.snp.makeConstraints {
            $0.center.equalToSuperview()
            $0.width.equalTo(100)
            $0.height.equalTo(100)
        }
        addSubview(recordingButton)
}

The solution is to change the location of addSubview.

private func setupRecordingButton() {
        recordingButton.setTitle("Record", for: .normal)
        recordingButton.backgroundColor = .yellow
        
        addSubview(recordingButton)
        recordingButton.snp.makeConstraints {
            $0.center.equalToSuperview()
            $0.width.equalTo(100)
            $0.height.equalTo(100)
        }
}

Recommended Posts

Error when using SnapKit
Error when using rails capybara
Error handling when Gradle dependency is using SLF4J
Error when bundle install
Error when deploying EC2
Error when playing with java
SpringBoot + Mybatis error when booting
Detailed tips when using Rails
Error when introducing SNS authentication
[Note] Error message when using Rails Form object pattern Japanese
I got an error when using nextInt, nextLine and substring.
[Rails] Japanese localization of error message when using Form object
Precautions when using checkboxes in Thymeleaf
Error when npm install on Windows 7
Workspace location when using Jenkins Pipeline
Be careful when using multiple articles
Error when deploying EC2 on CircleCI
[Practice! ] Minimum settings when using MyBatis
Solution notes when an error occurs when downloading docker gpg using curl
Unexpected exception when using Java DateTimeFormatter
Avoid character code error in java when using VScode extension RUN-CODE
How to solve the unknown error when using slf4j in Java
The story that the build error did not stop when using Eclipse 2020
About error when implementing spring validation
Unknown error in line 1 of pom.xml when using Spring Boot in Eclipse
[Visual Studio Code] I get a syntax error when debugging when using rbenv
Let's specify the version when using docker
Precautions when using checkstyle version 6.6 or higher
Error when trying to use heroku command
Error when building infrastructure with aws app
Error in implementation when implementing Spring validation
Check code compatibility using php7 compatibility when upgrading PHP
Using hidden type when PUT with Thymeleaf
Get the error message using the any? method
Error when starting eclipse after upgrading JDK
A reminder when an aapt.exe error occurs
Javaw.exe error when starting Spring Boot (STS)
Be careful when using rails_semantic_logger with unicorn
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
[Spring framework] HTTP request error handling by RestTemplate when using a proxy server
When using a list in Java, java.awt.List comes out and an error occurs