1

I have a problem with placing my stackView in the middle of my view controller which has a table view. I have created simple app with tabBarController and each tab bar item has its own navigationView Controller.

I created my views and placed them inside stackview.

private let myStackView: UIStackView = {
    let stackView = UIStackView()
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .vertical
    stackView.alignment = .center
    stackView.distribution = .equalSpacing
    stackView.spacing = 16

    return stackView
}()

private let myTitleLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .label
    label.textAlignment = .center
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.font = UIFont.systemFont(ofSize: 17, weight: .bold)

    return label
}()

private let mySubtitleLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .secondaryLabel
    label.textAlignment = .center
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.font = UIFont.systemFont(ofSize: 17)

    return label
}()

Inside viewDidLoad I added my views and activated constraints.

    myStackView.addArrangedSubview(myImageView)
    myStackView.addArrangedSubview(myTitleLabel)
    myStackView.addArrangedSubview(mySubtitleLabel)
    tableView.addSubview(myStackView)

    NSLayoutConstraint.activate([
        myStackView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
        myStackView.centerYAnchor.constraint(equalTo: self.centerYAnchor)
    ])

With a prefersLargeTitles set to false it looks like this enter image description here

but when I set my navigation bar to prefers large titles then my view is not centered. enter image description here

I want that stackView to be as a subview of my tableView, because I want this view to be scrollable. (I don't want to use tableView backgroundView, which is not scrollable) What am I doing wrong?

ShadeToD
  • 403
  • 9
  • 22
  • Already in the first screenshot doesn't seem to be centered vertically, like there is some space on top. But to successfully check whats going on, first thing you can do is to give more indication on the code, what is the viewcontroller where the constraint are being applied, can you show more code? Also, can you debug the app with Debug View Hierarchy while its running so you can clearly see your views and what is taking more space or less space. – denis_lor Mar 17 '20 at 16:25
  • Try looking at the view frames and active constraints in [the view debugger](https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html). – Yonat Mar 17 '20 at 16:39
  • 1
    `tableView.addSubview(myStackView)` You can’t add a subview to a table view, the whole idea makes no sense. A table view is a highly complex composite view with a lot of subviews of its own and a strict functionality; adding a subview to it manually just lames it. Moreover, no table is visible in your screen shot, so what are you even trying to do here? What's the table view even for? Finally, the notion of a scrollable stack view makes very little sense. Perhaps this is an x-y question and what you should really do is describe the overall desired interface. – matt Mar 17 '20 at 17:12
  • @matt I wanted to add State view to my table view. For example I will display some message with there is no data. TableView is a subclass of ScrollView so I thought that I can just add a subview to it. – ShadeToD Mar 18 '20 at 08:27

0 Answers0