質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

532閲覧

コードだけの画面生成でNSLayoutConstraint for <NSLayoutXAxisAnchor:というエラーが出る

momokoko

総合スコア38

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/06/25 14:35

編集2020/06/25 14:59

Storyboardを使わずコードだけで画面を生成してます。
こちらを参考にしました。
リンク内容

環境
xcode 11.5

シュミレーターを起動する段階で、以下のエラーが出ます。

Thread 1: Exception: "NSLayoutConstraint for <NSLayoutXAxisAnchor:0x28249a340 \"UILabel:0x10460b9e0'tom'.left\">: A constraint cannot be made between <NSLayoutXAxisAnchor:0x28249a340 \"UILabel:0x10460b9e0'tom'.left\"> and <NSLayoutXAxisAnchor:0x282499d40 \"UIView:0x104712570.leading\"> because their units are not compatible."

コンソールのエラーコード

koko test1 test2 2020-06-25 22:48:23.347324+0900 3CountUpSwfit[1243:258805] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for <NSLayoutXAxisAnchor:0x28249a340 "UILabel:0x10460b9e0'tom'.left">: A constraint cannot be made between <NSLayoutXAxisAnchor:0x28249a340 "UILabel:0x10460b9e0'tom'.left"> and <NSLayoutXAxisAnchor:0x282499d40 "UIView:0x104712570.leading"> because their units are not compatible.' *** First throw call stack: (0x1b20a5794 0x1b1dc7bcc 0x1b237db14 0x104151110 0x10414dc1c 0x10414dd28 0x1b5b04750 0x1b5b091e0 0x1b5b095e8 0x1b6183c50 0x1b618338c 0x1b61842ec 0x1b6195170 0x10414ea04 0x10414ec94 0x1b6142c74 0x1b6144a00 0x1b614a29c 0x1b58eebb0 0x1b61462e0 0x1b6146634 0x1b614c444 0x10414f620 0x1b1ea58f0) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

エラーが出るのコード

if let left = left { print("test2") self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true print("test3") }
self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true

をコメントアウトするとビルドできます。

ファイル全体のコード

import UIKit extension UIView{ // anchorを使うための準備コード func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?,right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight:CGFloat, width: CGFloat, height: CGFloat){ //AutoLayoutの有効化。UI部品の位置や大きさを指定できる translatesAutoresizingMaskIntoConstraints = false if let top = top { self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true print("test1") } if let left = left { print("test2") self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true print("test3") } if let bottom = bottom { self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true print("test4") } if let right = right { self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true print("test5") } if width != 0{ widthAnchor.constraint(equalToConstant: width).isActive = true } if height != 0{ heightAnchor.constraint(equalToConstant: height).isActive = true } } }

上記のコードは別のプロジェクトでは問題なくビルドできます。

下記の2ファイルを含め、3つのswiftファイルがプロジェクトのすべてです。

import UIKit class ViewController: UIViewController { let label: UILabel = { let ul = UILabel() ul.text = "tom" ul.backgroundColor = .blue return ul }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .blue print("koko") view.addSubview(label) label.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 40, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40) } }
import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let scene = (scene as? UIWindowScene) else { return }      ##書き加えた3文 window = UIWindow(windowScene: scene) window?.rootViewController = ViewController() window?.makeKeyAndVisible() }

検索してもよくわかない状態です。
ご教授いただけるとうれしいです

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

iphoneを抜き差ししたらビルドできました。

投稿2020/06/25 15:02

momokoko

総合スコア38

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問