I'm having a view hierarchy as follows. I'm a New Developer.
UIViewController->UIView->UIStackView->UIStackView-> (UILabel, UITextField, UIButton, etc.,). I'm creating everything programatically, execpt the UIViewController and UIView putting in storyboard. I'm using in a master-detail application. I've given constaints in storyboard. For my project Interface Builder is not enough, I need to handle everything programatically.
Using the above hierarchy, I'm putting lots of UILabels (A stack view in a table column to hold multiple UILabel). I can put other controls like UITextField, UIButton, etc., along side the UILabel's
I see the controls are going off the screen and it is not wrapping. In some case the UILabels are cut off without "..."
Please give or point me to a sample code to make the controlls wrapped!
// View refered from storyboard
@IBOutlet weak var customView: UIView!
// Main stack view holding a table
let tag: Int = 1
let stackView = UIStackView()
stackView.axis = NSLayoutConstraint.Axis.vertical
stackView.distribution = UIStackView.Distribution.fillProportionally
stackView.alignment = UIStackView.Alignment.leading
stackView.spacing = 16.0
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.isUserInteractionEnabled = true
stackView.tag = tag
tag += 1
// Loop through the rows
// Loop through columns
// For each column I add a sub stack view
let stackSubView = UIStackView()
stackSubView.axis = NSLayoutConstraint.Axis.horizontal
stackSubView.distribution = UIStackView.Distribution.fillProportionally
stackSubView.alignment = UIStackView.Alignment.leading
stackSubView.spacing = 5.0
stackSubView.isUserInteractionEnabled = true
stackSubView.translatesAutoresizingMaskIntoConstraints = false
stackSubView.tag = tag
tag += 1
// Loop through the column items
// If Label type:
// Label
let label = UILabel(frame: CGRect(x: CGFloat(xAxis), y: CGFloat(yAxis), width: CGFloat(width), height: CGFloat(height)))
label.text = "text value"
// if light
label.font = UIFont(name: "Helvetica", size: CGFloat(size))
// if bold
label.font = UIFont(name: "Helvetica-Bold", size: CGFloat(size))
// I'm incrementing the labelTag as per number of unique controls
label.tag = tag
// Truncating Trail not working and "..." are coming.
// I want full text. "..." is not human friendly
label.lineBreakMode = .byTruncatingTail
stackSubView.addArrangedSubview(label) // Add single UILabel in each sub stack view cell
// End of if
// If Text Field type:
// Text Field
let textField = UITextField(frame: CGRect(x: CGFloat(xAxis), y: CGFloat(yAxis), width: CGFloat(width), height: CGFloat(height)))
textField.text = "text field value"
textField.borderStyle = .line
textField.placeholder = "text field value"
label.lineBreakMode = .byTruncatingTail
// I'm incrementing the textFieldTag as per number of unique controls
textField.tag = tag
stackSubView.addArrangedSubview(textField) // Add single UITextField in each sub stack view cell
// End of if
// If Button Type:
// Button
let button = UIButton(frame: CGRect(x: CGFloat(xAxis), y: CGFloat(yAxis), width: CGFloat(width), height: CGFloat(height)))
let value = "button value"
button.setTitle(value, for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.isUserInteractionEnabled = true
// I'm incrementing the buttonTag as per number of unique controls
button.tag = tag
stackSubView.addArrangedSubview(button) // Add single UIButton in each sub stack view cell
// End of if
tag += 1
// End of column item loop
stackView.addArrangedSubview(stackSubView)
// End of column loop
// End of row loop
// View Controller
customView.addSubview(stackView)
self.view.addSubview(customView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20)
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 20).isActive = true