问题描述
我正在使用Storyboards进行100天的Swift 工作,并且遇到了一些问题,几乎完全将UI View推到了UINavigationController下。删除UINavigationController时,一切都会按预期进行。我试图添加一个新视图,设置edgesForExtendedLayout =[]
,并将我的CGRect的y
调整为无效。我几乎可以肯定我正在做一些愚蠢的事情,但是我一生都无法弄清楚是什么。
预计这3个按钮将从navView开始显示。
SceneDelegate.swift:
func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionoptions: UIScene.Connectionoptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
let viewController = ViewController()
window?.rootViewController = UINavigationController(rootViewController: viewController)
window?.makeKeyAndVisible()
window?.windowScene = windowScene
}
ViewController.swift:
class ViewController: UIViewController {
var button1: UIButton!
var button2: UIButton!
var button3: UIButton!
var countries = [String]()
var score = 0
var correctAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
button1 = UIButton(type: .custom)
button1.setimage(UIImage(named: "us"),for: .normal)
button1.translatesAutoresizingMaskIntoConstraints = false
button1.frame = CGRect(x: 100,y: 100,width: 200,height: 100)
button1.layer.borderWidth = 1
button1.layer.borderColor = UIColor.lightGray.cgColor
view.addSubview(button1)
button2 = UIButton(type: .custom)
button2.setimage(UIImage(named: "us"),for: .normal)
button2.translatesAutoresizingMaskIntoConstraints = false
button2.frame = CGRect(x: 100,y: 230,height: 100)
button2.layer.borderWidth = 1
button2.layer.borderColor = UIColor.lightGray.cgColor
view.addSubview(button2)
button3 = UIButton(type: .custom)
button3.setimage(UIImage(named: "us"),for: .normal)
button3.translatesAutoresizingMaskIntoConstraints = false
button3.frame = CGRect(x: 100,y: 360,height: 100)
button3.layer.borderWidth = 1
button3.layer.borderColor = UIColor.lightGray.cgColor
view.addSubview(button3)
countries += ["estonia","france","germany","ireland","italy","monaco","nigeria","poland","russia","spain","uk","us"]
askQuestion()
}
func askQuestion() {
countries.shuffle()
correctAnswer = Int.random(in: 0...2)
button1.setimage(UIImage(named: countries[0]),for: .normal)
button2.setimage(UIImage(named: countries[1]),for: .normal)
button3.setimage(UIImage(named: countries[2]),for: .normal)
title = countries[correctAnswer].uppercased()
}
}
解决方法
根据OP的评论...
问题在于您要设置的每个按钮:
button1.translatesAutoresizingMaskIntoConstraints = false
但随后尝试显式设置按钮框架:
button1.frame = CGRect(x: 100,y: 100,width: 200,height: 100)
您要 使用自动布局或显式框架。混合它们会导致麻烦,UI元素不会显示在您期望的位置(如您所见)。
,由于您在不使用情节提要的情况下构建视图。您应该添加约束以编程方式放置按钮。
将下面的代码添加到viewDidLoad
func
button1.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.init(item: button1!,attribute: .top,relatedBy: .equal,toItem: view.safeAreaLayoutGuide,multiplier: 1,constant: 0).isActive = true
或者,
button1.translatesAutoresizingMaskIntoConstraints = false
button1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
他们做同样的事情-以编程方式将约束从button1
添加到顶部safeArea
。您还可以根据需要向button2和button3添加约束。