I am trying to create square search bar but when I set width less than 116, search icon and textfield area moved out of search bar. Please refer first search bar of screenshot.
I have fixed this issue by adding following code to set constraints manually for searchTextField.
searchBar.searchTextField.translatesAutoresizingMaskIntoConstraints = false
searchBar.searchTextField.leftAnchor.constraint(equalTo: searchBar.leftAnchor, constant: 8).isActive = true
searchBar.searchTextField.rightAnchor.constraint(equalTo: searchBar.rightAnchor, constant: -8).isActive = true
searchBar.searchTextField.topAnchor.constraint(equalTo: searchBar.topAnchor, constant: 6).isActive = true
searchBar.searchTextField.bottomAnchor.constraint(equalTo: searchBar.bottomAnchor, constant: -6).isActive = true
While expanding width, I marked translatesAutoresizingMaskIntoConstraints as true.
searchBar.searchTextField.translatesAutoresizingMaskIntoConstraints = true
Related
if I set fixed width it either appears too large for small screen (4.5 in) or too small for large screen (6.5 in)
and
is there any special way to ensure the constraints hold good in all
constraints
Like Jatin mentioned in the comments, you can use leading and trailing anchors relative to the view like this,
textField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
textField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
Or, you could set the width as a multiplier to the width of the view.
textField.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.80).isActive = true
Note: Change the constant and multiplier values to suit your needs.
I can't see the menu bar (The blue view) when i write this code, but when i change the parameter from 50 to 100 its shows. It seems like it lies behind the status field. I want the constraints to relate to the status bar not the screens top. Someone who knows why?
func setupMenuBar(){
view.addSubview(menuBar)
view.addConstriantswithFormat(format: "H:|[v0]|", views:menuBar)
view.addConstriantswithFormat(format: "V:|[v0(50)]", views:menuBar)
}
You need to constrain your menuBar view to the view's safe area to get it to align with the bottom of the navigation bar.
Tough to do with Visual Format Language though. This alternative method should be easy to understand:
view.addSubview(menuBar)
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
menuBar.topAnchor.constraint(equalTo: guide.topAnchor, constant: 0.0),
menuBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 0.0),
menuBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: 0.0),
menuBar.heightAnchor.constraint(equalToConstant: 50.0),
])
If the red view is a navigation bar, you should probably look at not extending edges under top bar; otherwise, you need to constrain the blue view in accordance to the red view.
I prefer using the anchors. It is much easier to read.
For example (Swift):
blueView.topAnchor.constraint(equalTo: redView.bottomAnchor).isActive = true
blueView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
blueView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
blueView.heightAnchor.constraint(equalToConstant: 50).isActive = true
I have a label called dollarLabel in custom table view cell, and set its auto layout constraints and appearance as following code snippets:
// Dollar label constraint.
dollarLabel.translatesAutoresizingMaskIntoConstraints = false
dollarLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
dollarLabel.leadingAnchor.constraint(equalTo: subCategoryLabel.trailingAnchor, constant: 8).isActive = true
dollarLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8).isActive = true
dollarLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8).isActive = true
dollarLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.3).isActive = true
// Dollar label appearance.
dollarLabel.textAlignment = NSTextAlignment.right
dollarLabel.font = UIFont.boldSystemFont(ofSize: 30)
dollarLabel.adjustsFontSizeToFitWidth = true
And the cells are shown as below:
I’m wondering why the dollar symbol of the third row is not aligned as the first and the second dollar symbol?
I set its textAlignment property to NSTextAlignment.right, and I expect that the three dollar symbol should be aligned vertically when the digital number have the same length.
What’s going wrong about my code?
Many thanks!
I'm using the following code to constrain a view to the left and right anchors of a parent UIScrollView.
Despite the right anchor and the left anchor being set to the ScrollView's left and right anchors, the view does not expand to fill the scrollview.
Note: The gray background in this image is the UIScrollView's background, so I know that's properly constrained to its parent view.
Code:
self.wtfView.translatesAutoresizingMaskIntoConstraints = false
self.wtfView.backgroundColor = UIColor.orange
self.wtfView.topAnchor.constraint(equalTo: self.passwordField.bottomAnchor, constant: 40.0).isActive = true
self.wtfView.leftAnchor.constraint(equalTo: self.containerView.leftAnchor, constant: 40.0).isActive = true
self.wtfView.rightAnchor.constraint(equalTo: self.containerView.rightAnchor, constant: 40.0).isActive = true
self.wtfView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
self.wtfView.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 40.0).isActive = true
https://imgur.com/a/U88iW
Edit:
The following code works correctly, but I would prefer to use the left+right anchor technique to specify the width, and not at a width constraint. Shouldn't that be possible?
self.wtfView.translatesAutoresizingMaskIntoConstraints = false
self.wtfView.backgroundColor = UIColor.orange
self.wtfView.topAnchor.constraint(equalTo: self.passwordField.bottomAnchor, constant: 40.0).isActive = true
self.wtfView.leftAnchor.constraint(equalTo: self.containerView.leftAnchor, constant: 40.0).isActive = true
self.wtfView.widthAnchor.constraint(equalTo: self.containerView.widthAnchor, constant: -80.0).isActive = true //THE DIFFERENT ONE
self.wtfView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
self.wtfView.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 040.0).isActive = true
The reason for this is that the contentView of the UIScrollView still doesn't know that you want it to take up the width of it's parentView.
You can fix this by adding the following constraint in iOS11:
self.containerView.contentLayoutGuide.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
This says "Hey, I want you to lock the content Width to the width of the superview.
Pre iOS 11 you can simply constrain a subview to both the parent view's left and right anchors AND the content view's left and right anchors.
Like so:
self.wtfView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 40.0).isActive = true
self.wtfView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 40.0).isActive = true
Much like, Aleksei's recommendation you are now constraining the width to a rigid value ( the width of the parent view ), and the scrollview will use that to decide the width of the scrollview.
may be try to provide:
self.wtfView.widthAnchor.constraint(equalTo: self.containerView.widthAnchor, constant: -40.0).isActive = true
I am trying to learn how to apply constraints to build a UITableViewCell. I am using iOS 10 using Swift. Here is my requirement.
I want to apply constraints to the green control which is a button and make sure that it is on the right hand side as shown above.
countView.widthAnchor.constraint(equalToConstant: 100).isActive = true
countView.heightAnchor.constraint(equalToConstant: 44).isActive = true
I have supplied the width and height constraints as above. But I don't know how to put it on the right hand side of the cell with some space as a margin.
UPDATE: I don't see anything on the screen.
countView.backgroundColor = UIColor.green
self.addSubview(countView)
countView.widthAnchor.constraint(equalToConstant: 100).isActive = true
countView.heightAnchor.constraint(equalToConstant: 44).isActive = true
countView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 20).isActive = true
countView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true