自定义 UIView 不会占用整个屏幕宽度
我已经为此绞尽脑汁好几天了,找不到问题所在 -.-
我有一个 MainActionButton
这是我创建的一个 自定义 UIView
。这些是我定义给它的约束:
[]
我需要它作为注册和登录按钮,一个在另一个之下。这就是它的应该看起来的样子:
第一个问题我遇到的情况是,由于某种原因,除非我将每个按钮放入其自己的 StackView 中 - 它们拒绝放置在我告诉它们的位置。只有在 stackView 内部,它们才会站在屏幕底部。
当前问题 - 尽管 stackView 被定义为与屏幕两侧对齐(每侧 20 边距),但自定义 UIView 坚持不采用完整宽度,因为它们应该如此! 它看起来像这样:
- 我的自定义 ui 代码非常通用:
protocol MainActionButtonDelegate {
func actionButtonDidPress(btnText: String)
}
class MainActionButtonView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var entireButton: UIView!
@IBOutlet weak var buttonLabel: UILabel!
@IBOutlet weak var buttonIcon: UIImageView!
var delegate: MainActionButtonDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.translatesAutoresizingMaskIntoConstraints = false
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
self.frame = self.bounds
}
func commonInit() {
Bundle.main.loadNibNamed("MainActionButtonView", owner: self, options: nil)
addSubview(contentView)
contentView.layer.cornerRadius = contentView.frame.size.height / 2
contentView.clipsToBounds = true
setGestureRecognizer()
}
func setGestureRecognizer() {
contentView.addGestureRecognizer(UITapGestureRecognizer(target: contentView, action: #selector(buttonTapped)))
contentView.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(buttonTapped(tapGestureRecognizer:)))
contentView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func buttonTapped(tapGestureRecognizer: UITapGestureRecognizer) {
delegate?.actionButtonDidPress(btnText: buttonLabel.text!)
}
}
什么是我做错了,我该如何解决这个问题?这里快要疯了!
PS 我正在使用情节提要,
我尝试以编程方式在屏幕的 ViewController 的 viewDidLoad 上添加不同的约束,但似乎没有任何效果对我有用。
我最近尝试的示例:
topButton.contentView.widthAnchor.constraint(equalToConstant: CGFloat(UIScreen.main.bounds.width)).isActive = true
编辑#1:这就是屏幕层次图中的样子: 在此处输入图片说明 我可以看到我的 StackView (我被迫添加它以便能够放置按钮)确实是屏幕的宽度,但是自定义 UIView 的视图没有获得相同的宽度(即使在其快速的内部)文件我将其限制为与超级视图的前导和尾随对齐)
I've been cracking my head around this for days now and can't find what the issue is -.-
I have a MainActionButton
which is a custom UIView
that I created. These are the constraints I defined to it:
[]
I need it to appear twice on the page as a signup and login buttons, one under the other. This is how it is supposed to look:
First problem I encountered was that for some reason, unless I put each button inside a StackView of its own - they refuse to be placed where I tell them to. Only inside stackViews they obey to stand at the bottom of the screen.
Current issue - even though the stackView is define to be aligned to the sides of the screen (with 20 margin each side) the custom UIView insist not to take on the full width as they should!
This is what it looks like:
- My code of the custom ui is super generic:
protocol MainActionButtonDelegate {
func actionButtonDidPress(btnText: String)
}
class MainActionButtonView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var entireButton: UIView!
@IBOutlet weak var buttonLabel: UILabel!
@IBOutlet weak var buttonIcon: UIImageView!
var delegate: MainActionButtonDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.translatesAutoresizingMaskIntoConstraints = false
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
self.frame = self.bounds
}
func commonInit() {
Bundle.main.loadNibNamed("MainActionButtonView", owner: self, options: nil)
addSubview(contentView)
contentView.layer.cornerRadius = contentView.frame.size.height / 2
contentView.clipsToBounds = true
setGestureRecognizer()
}
func setGestureRecognizer() {
contentView.addGestureRecognizer(UITapGestureRecognizer(target: contentView, action: #selector(buttonTapped)))
contentView.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(buttonTapped(tapGestureRecognizer:)))
contentView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func buttonTapped(tapGestureRecognizer: UITapGestureRecognizer) {
delegate?.actionButtonDidPress(btnText: buttonLabel.text!)
}
}
What am I doing wrong and how can I solve this? Going insane here!
P.S I am using the storyboard
I tried adding different constraints programmatically on the viewDidLoad of the screen's ViewController, but nothing seems to work for me.
Example of my latest attempt:
topButton.contentView.widthAnchor.constraint(equalToConstant: CGFloat(UIScreen.main.bounds.width)).isActive = true
Edit #1: This is what it looks like in the hierarchy diagram of the screen:
enter image description here
I can see that my StackView (that I was forced to add to be able to place the button) is indeed the width of the screen, but then the View of the custom UIView doesn't get the same width (even though inside its swift file I constrained it to align to the leading and trailing of the superview)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论