I have two views, one UIView and one UIStackView
let mainView = UIView()
mainView.translatesAutoresizingMaskIntoConstraints = false
let stackView = UIStackView()
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false
They are both added to the superView with these constraints
NSLayoutConstraint.activate([
mainView.topAnchor.constraint(equalTo: view.topAnchor),
mainView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mainView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.topAnchor.constraint(equalTo: mainView.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
When both the UIView and the UIStackView doesn't hold any other views, I get warnings on both views in the debugger
Height is ambiguous for uiview
Height and vertical position is ambiguous for uistackview
But if I add a UIButton to the UIStackView, and then set it to hidden with .isHidden = true, both warnings are gone, except for iOS10 where both warnings remain.
With or without the debugger warnings I get the result I want, that if the UIStackView is empty (or all subviews hidden), the UIView covers the whole screen.
What is going on here, why is one hidden view not the same as an empty stackView, and why does the behavior differ on iOS10? And last but not least, how can I satisfy iOS10 without compromising my current layout, that seemingly works for everything after iOS10?
Here's what you got — mainView and stackView are have no heights and edges are clipped.
There's no way to calculate vertical position and height base on that constraints. You can add some height constraint on either view to make it clear for auto layout
mainView.heightAnchor.constraint(greaterThanOrEqualToConstant: 1)
Related
How should I setup auto layout constrains so that multiline label stays vertically centered inside scrollview until it's text content becomes too long to be shown at once? When the text length becomes too long text should be aligned at top with the scrollview so the user can see the beginning of text and scroll for more. This is how I tried to setup constrains
for scrollView:
Equal Height to: Superview
Align Trailing to: Safe Area, Equals = -8
Align Leading to: Safe Area, Equals = 8
Align Top to: Safe Area
for label:
Leading Space to: Superview
Equal Width to: Superview
Align Center Y to: Superview
I also added following code to viewDidLoad()
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
The problem is that but I still get some warnings and also text ends up "vertically centered" inside scrollview without possibility to really scroll to the beginning or the end of it, even when it can not fit whole inside. For scrollview I get warning that it "has ambiguous scrollable content width", while for the label I get warning "trailing constraint is missing, which may cause overlapping with other views"
How about constraining the label height to the height of the scrollView's superview? In that case the label will be always as big as the screen where it is presented, and since by default the text in a UILabel is centered vertically, you would get what you want. See the following Playgrounds example for reference:
import PlaygroundSupport
import UIKit
class A: UIViewController {
let scrollView = UIScrollView()
let label = UILabel()
override func viewDidLoad() {
self.view.backgroundColor = .white
self.view.addSubview(scrollView)
self.scrollView.addSubview(label)
label.numberOfLines = 0
label.text = "How should I setup auto layout constrains so that multiline label stays vertically centered inside scrollview until it's text content becomes too long to be shown at once?"
scrollView.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
scrollView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
scrollView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
label.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
label.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
label.topAnchor.constraint(equalTo: self.scrollView.topAnchor),
label.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
label.heightAnchor.constraint(greaterThanOrEqualTo: self.view.heightAnchor),
])
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = A()
I have a view that is composed of an image, a form with 11 UITextfield and a button, but the form is too big for my screen that is why I tried to use a UIScrollview.
The error I have is that my UIScrollview does not work as I can solve this problem.
This is my code:
import UIKit
class LoginCtrl: UIViewController {
let scrollView: UIScrollView = {
let sv = UIScrollView(frame: UIScreen.main.bounds)
sv.isScrollEnabled = true
sv.contentSize = CGSize(width: 2000, height: 5678)
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(r: 0, g: 150, b: 136)
self.view.addSubview(scrollView)
scrollView.addSubview(contenedorCampos)
setear_posicion_scrollView()
setear_posicion_contenedor()
}
func setear_posicion_scrollView(){
//definir x,y,width,height constraints
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
var heightContenedor: NSLayoutConstraint?
func setear_posicion_contenedor(){
//definir x,y,width,height constraints
contenedorCampos.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
contenedorCampos.topAnchor.constraint(equalTo: tabsInicio.bottomAnchor, constant: 12).isActive = true
contenedorCampos.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
heightContenedor = contenedorCampos.heightAnchor.constraint(equalToConstant: 400)
heightContenedor?.isActive = true
contenedorCampos.addSubview(txtNombres)
contenedorCampos.addSubview(divider_txtNombres)....
}
}
Thanks
You've defined the relationship between the scrollview and its superview(which defines its frame), but not the relationship between the scrollview and its subviews (which defines its contentSize). As a result, the actual contentSize of the scrollview will be just be (0, 0).
In other words, you never actually laid anything out, at least not in the code you posted.
What you need to do is define layout constraints for the actual child views (everything that is a subview of the scrollview). Make sure to set up constraints definitively pinning these components to the edges of their parent (the scrollview). Once you have defined the constraints sufficiently, the scrollview should have a content size.
Technical note about this
In general, Auto Layout considers the top, left, bottom, and right
edges of a view to be the visible edges. That is, if you pin a view to
the left edge of its superview, you’re really pinning it to the
minimum x-value of the superview’s bounds. Changing the bounds origin
of the superview does not change the position of the view.
The UIScrollView class scrolls its content by changing the origin of
its bounds. To make this work with Auto Layout, the top, left, bottom,
and right edges within a scroll view now mean the edges of its content
view.
The constraints on the subviews of the scroll view must result in a
size to fill, which is then interpreted as the content size of the
scroll view. (This should not be confused with the
intrinsicContentSize method used for Auto Layout.) To size the scroll
view’s frame with Auto Layout, constraints must either be explicit
regarding the width and height of the scroll view, or the edges of the
scroll view must be tied to views outside of its subtree.
I couldn't get my UIScrollView to scroll.
Here is my code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView(frame : CGRect ( x:0,y:0,width:UIScreen.main.bounds.width,height:UIScreen.main.bounds.height))
scrollview.delegate = self
view.addSubview(scrollView)
for i in 0...14 {
let numLabel = UILabel(frame : CGRect( x : 0 , y : 10+(i*40) , width : UIScreen.main.bounds.width - 20 : height : 40))
numlabel.text = "\(i)"
scrollView.addSubview(numLabel)
}
}
}
This is making the views appear but not scrolling.
Scroll view scrolls to its content size.
Whenever you add a subview to scroll view you should make sure that your scroll view's content size is enough to fit the new view.
In your case you are not taking care of that.
Ideally whenever you add a subview you should correspondingly adjust the hight of the scroll view content size.
In your case after you have added all of the labels to scroll view i.e. after for loop add following line
self.scrollView.contentSize = CGSize(width:self.scrollView.bounds.size.width,height:10+(15*40))
or in the for loop after adding label you can do the following
self.scrollView.contentSize = CGSize(width:self.scrollView.bounds.size.width,height:10+((i+1)*40))
The second approach is better. Because if you add more labels to scroll view it will take care of that. Again, make it a rule of thumb, whenever adding a view to scroll view make sure that its content size is updated to fit all the subviews.
While you can manually set the contentSize, I would not advise doing that.
Instead, I'd use constraints for the subviews of the scroll view. The auto-layout engine will calculate the contentSize for you automatically. It will also take care of adjusting everything if the device rotates.
I'd also suggest using a stack view, you don't have to mess around with either manual frames for the labels nor with constraints between them.
So, you can do something like:
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
let stackView = UIStackView()
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(stackView)
for i in 0 ... 140 {
let numLabel = UILabel()
numLabel.translatesAutoresizingMaskIntoConstraints = false
numLabel.text = "\(i)"
numLabel.font = .preferredFont(forTextStyle: .body)
stackView.addArrangedSubview(numLabel)
}
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
])
}
Note the use of UIFont.preferredFont(forTextStyle: .body) to enjoy Dynamic Text. This means that if the user has a larger font specified in their settings, this will automatically show the larger font in this app. But more importantly, we didn't have to calculate the size of the label for that font. Constraints and the stack view took care of both the frames of the labels as well as the scroll view's contentSize.
For the sake of completeness, it's worth noting that the alternative is to use a UITableView or UICollectionView. This is a scalable, memory efficient way of viewing data within a scroll view. It's beyond the scope of this question, but it's worth remembering as you consider creating large scroll views.
I have one stack view that contains with 4 buttons. And each button I also add subview to that. The subview of that 4 buttons, I try to program to add constraint into it. Some constraint such as .Trailing .Leading .Top .Bottom I cannot add to it by error constraint and stack view problem. How any solution to add that constraints to subview of stackview. if have any sample it's really good for me. thank in advance
UIStackView's power is to reduce your using of constrains, just give it some setting info such as axis, distribution, alignment, spacing. stack view will layout your subview item automatically, cause stack view's size is based on its' subviews' intrinsicContentSize, you can set subview's size by extra constraints to override.
adding constraints to subview of stackView is the same as other items in UIView. but is not a StackView way, and you should be care about adding conflict constraints.
hope this code demo helps:
let stackView = UIStackView()
let demoView = UIView()
demoView.backgroundColor = UIColor.red
stackView.addArrangedSubview(demoView)
demoView.translatesAutoresizingMaskIntoConstraints = false
// add your constraints as usual
demoView.widthAnchor.constraint(equalToConstant: 300).isActive = true
demoView.heightAnchor.constraint(equalToConstant: 200).isActive = true
demoView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
demoView.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
I have a Screen like this which is built in interface builder:
The control at the bottom is a UICollectionView and the other two are a UIButton and a segmentedControl. They sit on a UIView which is a child of the UIController's view but has the same frame. In my code, I add the UIViewController to a UINavigationController so there will be a UINavigationBar on the top of the screen. I can set the auto layout constraint to force the top of this screen to move below the navigationbar. However when I scroll down the UICollectionView I couldn't scroll to the bottom to view the rest of the items. I can only see half size of the last two items.
I have updated the code and put some log in viewDidAppear:
UICollectionView frame height = 527.000000
View frame = 504.000000
So the UICollectionView's height is bigger than the view's height. I want the collection view to fit right in both 3.5" screen and 4" screen.
Any idea what I have done wrong? How can I fix this?
This is because your collection view's height is exceeding that of the frame, try to reduce the height of your collection view which fits that of your UIView's height - your collection view's yOrigin.
The problem is that when the navigation bar is added, the UICollectionView gets "pushed" down but its height remains the same.
You need to add a layout constraint that will cause the UICollectionView to reduce height when adding the navigation bar. The constraint should be something along the lines of "bottom space to bottom layout guide = 0"
Swift 4.1
Try to define the constraints as follows to consider the Navigation Bar (this should work even with autorotation, when the status bar can change in height) It also considers the chante in iOS 11 for the safeArea.
let collectionView = UICollectionView()
self.view.addSubview(collectionView)
if #available(iOS 11.0, *) {
let safeArea = self.view.safeAreaLayoutGuide
collectionView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 0).isActive = true
} else {
let topGuide = self.topLayoutGuide
collectionView.topAnchor.constraint(equalTo: topGuide.bottomAnchor, constant: 0).isActive = true
}
collectionView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
collectionView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true