Я занимаюсь разработкой небольшого приложения и хочу реализовать представление в расширении клавиатуры, как показано на следующем рисунке:
Теперь мой Приложение выглядит следующим образом:
Я хочу реализовать ограничения верхней и нижней кнопок, но у меня возникают проблемы, поскольку он создает вертикальную прокрутку, несмотря на увеличение высота пространства над клавиатурой, поскольку кнопки также увеличивают свою высоту:
Мой действительный код:
class CatboardBanner: ExtraView {
var scrollView = UIScrollView()
var stack = UIStackView()
let button = UIButton()
let button2 = UIButton()
let button3 = UIButton()
let button4 = UIButton()
let button5 = UIButton()
required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) {
super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
self.backgroundColor = UIColor.white.withAlphaComponent(0.01)
self.addSubview(scrollView)
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.addSubview(self.stack)
self.stack.translatesAutoresizingMaskIntoConstraints = false
self.stack.axis = .horizontal
self.stack.spacing = 10
button.setTitle("btn 1", for: .normal)
button.backgroundColor = UIColor.red
button.roundedCorners(rounding: 5)
button.translatesAutoresizingMaskIntoConstraints = false
button2.setTitle("btn 2", for: .normal)
button2.backgroundColor = UIColor.gray
button2.roundedCorners(rounding: 5)
button2.translatesAutoresizingMaskIntoConstraints = false
button3.setTitle("btn 3", for: .normal)
button3.backgroundColor = UIColor.brown
button3.roundedCorners(rounding: 5)
button3.translatesAutoresizingMaskIntoConstraints = false
button4.setTitle("btn 4", for: .normal)
button4.backgroundColor = UIColor.yellow
button4.roundedCorners(rounding: 5)
button4.translatesAutoresizingMaskIntoConstraints = false
button5.setTitle("btn 5", for: .normal)
button5.backgroundColor = UIColor.yellow
button4.roundedCorners(rounding: 5)
button5.translatesAutoresizingMaskIntoConstraints = false
self.stack.addArrangedSubview(button)
self.stack.addArrangedSubview(button2)
self.stack.addArrangedSubview(button3)
self.stack.addArrangedSubview(button4)
self.stack.addArrangedSubview(button5)
self.updateAppearance()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setNeedsLayout() {
super.setNeedsLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
self.scrollView.showsHorizontalScrollIndicator = false
self.scrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true;
self.scrollView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true;
self.scrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true;
self.scrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true;
self.stack.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor).isActive = true
self.stack.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
self.stack.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor).isActive = true
self.stack.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor).isActive = true
self.stack.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
let leadingConstraint = NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.stack, attribute: .leading, multiplier: 1, constant: 10)
let widthConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let heightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30)
let widthConstraint2 = NSLayoutConstraint(item: button2, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let heightConstraint2 = NSLayoutConstraint(item: button2, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30)
let widthConstraint3 = NSLayoutConstraint(item: button3, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let heightConstraint3 = NSLayoutConstraint(item: button3, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30)
let widthConstraint4 = NSLayoutConstraint(item: button4, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let heightConstraint4 = NSLayoutConstraint(item: button4, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30)
let widthConstraint5 = NSLayoutConstraint(item: button5, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
let heightConstraint5 = NSLayoutConstraint(item: button5, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30)
NSLayoutConstraint.activate([leadingConstraint, widthConstraint, heightConstraint, widthConstraint2, heightConstraint2, widthConstraint3, heightConstraint3, widthConstraint4, heightConstraint4, widthConstraint5, heightConstraint5])
}
@objc func respondToSwitch() {
}
func updateAppearance() {
}
}
ExtraView - это вид в расширении клавиатуры, где я работаю, и его высота равна 50.
На третьем изображении я попытался изменить ограничение topAnchor элемента scrollView на 8, но, к сожалению, это не так потренироваться.
Надеюсь, вы поможете мне с этой проблемой. Спасибо!