Как правильно настроить привязку к высоте? - PullRequest
0 голосов
/ 08 октября 2018

В моем MainVC я пытаюсь ограничить UIView вверху, влево, вправо и иметь высоту 80. Прямо сейчас мой вид на весь экран.Как бы я исправить свой код, чтобы иметь правильный размер?

// Переменные var topViewCons: [NSLayoutConstraint] = []

// Constants
let topGradient = RadialGradientLayer()
let topMainView = UIView()
// MainVC Top View Constraints
topMainView.translatesAutoresizingMaskIntoConstraints = false
topGradient.frame = view.bounds
topMainView.layer.addSublayer(topGradient)
self.view.addSubview(topMainView)

let topConstraint = topMainView.topAnchor.constraint(equalTo: self.view.topAnchor)
let leftConstraint = topMainView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let rightConstraint = topMainView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
let topViewHeight = topMainView.heightAnchor.constraint(equalToConstant: 80)
NSLayoutConstraint.activate([topConstraint, leftConstraint, rightConstraint, topViewHeight])

1 Ответ

0 голосов
/ 08 октября 2018

Это не полный экран, градиент

topGradient.frame = view.bounds  // here you make it's frame to screen bounds 
topMainView.layer.addSublayer(topGradient)

, поэтому вам необходимо установить

topMainView.clipsToBounds = true

ИЛИ

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  topGradient.frame = topMainView.bounds
}

Также вы можете сделать это напрямую, не используя

NSLayoutConstraint.activate([
      topMainView.topAnchor.constraint(equalTo: self.view.topAnchor),
      topMainView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
      topMainView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
      topMainView.heightAnchor.constraint(equalToConstant: 80)
])
...