How to animate a view to hide behind Navigation Bar - ios

I'm trying to animate a view to hide behind the navigation bar.
The idea is the yellow label to appear from behind the green view.
I tried this modifying the top constraint to a negative number, but it works but if the yellow view is bigger than the green one it ends over the safe area.
My code:
#IBAction func buttonClick(_ sender: Any) {
UIView.animate(withDuration: 0.5) {
if(self.topMargin.constant<0){
self.topMargin.constant=0
}else {
self.topMargin.constant = -100
}
self.view.layoutIfNeeded()
}
}
Thats the result when hidden:
How can I achieve this effect without invading the safe zone?

setup your label under your Controller class:
let myLabel: UILabel = {
let label = UILabel()
label.text = "Label"
label.backgroundColor = .darkYellow
label.textAlignment = .center
label.textColor = .black
label.font = .systemFont(ofSize: 16, weight: .semibold)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
After that set two variables for label animation state:
var labelUp: NSLayoutConstraint!
var labeldown: NSLayoutConstraint!
In viewDidLoad setup your nav bar (I set it with my extension), present your label and constraints:
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "label UP", style: .plain, target: self, action: #selector(self.handleAnimate))
view.addSubview(myLabel)
labeldown = myLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
labeldown.isActive = true
labelUp = myLabel.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
myLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
myLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
myLabel.heightAnchor.constraint(equalToConstant: 100).isActive = true
Now add a variable to control state of label and animation func:
var controlStatusLabel = true
#objc fileprivate func handleAnimate() {
switch controlStatusLabel {
case true:
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "label Down", style: .plain, target: self, action: #selector(self.handleAnimate))
self.labeldown.isActive = false
self.labelUp.isActive = true
self.view.layoutIfNeeded()
self.controlStatusLabel = false
}, completion: nil)
default:
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "label UP", style: .plain, target: self, action: #selector(self.handleAnimate))
self.labelUp.isActive = false
self.labeldown.isActive = true
self.view.layoutIfNeeded()
self.controlStatusLabel = true
}, completion: nil)
}
}
The result:

Embed your label (or whatever view you want to animate) in a "holder" view, constrained to the safe-area, with .clipsToBounds = true...
The holder view background will normally be clear -- I'm toggling it between clear and red so you can see it's frame.
Here'a quick example code for that:
class ViewController: UIViewController {
let label = UILabel()
let labelHolderView = UIView()
var labelTop: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Nav Bar"
// configure the label
label.textAlignment = .center
label.text = "I'm going to slide up."
label.backgroundColor = .systemYellow
// add it to the holder view
labelHolderView.addSubview(label)
// prevents label from showing outside the bounds
labelHolderView.clipsToBounds = true
label.translatesAutoresizingMaskIntoConstraints = false
labelHolderView.translatesAutoresizingMaskIntoConstraints = false
// add holder view to self.view
view.addSubview(labelHolderView)
// constant height for the label
let labelHeight: CGFloat = 160.0
// setup label top constraint
labelTop = label.topAnchor.constraint(equalTo: labelHolderView.topAnchor)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// activate label top constraint
labelTop,
// constrain label Leading/Trailing to holder
// we'll inset it by 20-points so we can see the holder view
label.leadingAnchor.constraint(equalTo: labelHolderView.leadingAnchor, constant: 20.0),
label.trailingAnchor.constraint(equalTo: labelHolderView.trailingAnchor, constant: -20.0),
// constant height
label.heightAnchor.constraint(equalToConstant: labelHeight),
// label gets NO Bottom constraint
// constrain holder to safe area
labelHolderView.topAnchor.constraint(equalTo: g.topAnchor),
labelHolderView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
labelHolderView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
// constant height (same as label height)
labelHolderView.heightAnchor.constraint(equalTo: label.heightAnchor),
])
// let's add a button to animate the label
// and one to show/hide the holder view
let btn1 = UIButton(type: .system)
btn1.setTitle("Animate It", for: [])
btn1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(btn1)
let btn2 = UIButton(type: .system)
btn2.setTitle("Toggle Holder View Color", for: [])
btn2.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(btn2)
NSLayoutConstraint.activate([
// put the first button below the holder view
btn1.centerXAnchor.constraint(equalTo: g.centerXAnchor),
btn1.topAnchor.constraint(equalTo: labelHolderView.bottomAnchor, constant: 20.0),
// second button below it
btn2.centerXAnchor.constraint(equalTo: g.centerXAnchor),
btn2.topAnchor.constraint(equalTo: btn1.bottomAnchor, constant: 20.0),
])
// give the buttons an action
btn1.addTarget(self, action: #selector(animLabel(_:)), for: .touchUpInside)
btn2.addTarget(self, action: #selector(toggleHolderColor(_:)), for: .touchUpInside)
}
#objc func animLabel(_ sender: Any?) {
// animate the label up if it's down, down if it's up
labelTop.constant = labelTop.constant == 0 ? -label.frame.height : 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
#objc func toggleHolderColor(_ sender: Any?) {
labelHolderView.backgroundColor = labelHolderView.backgroundColor == .red ? .clear : .red
}
}

Related

Weird horizontal shrinking animation when hiding UIButton with Configuration in UIStackView

I'm facing this weird animation issues when hiding UIButton in a StackView using the new iOS 15 Configuration. See playground:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
private weak var contentStackView: UIStackView!
override func viewDidLoad() {
view.frame = CGRect(x: 0, y: 0, width: 300, height: 150)
view.backgroundColor = .white
let contentStackView = UIStackView()
contentStackView.spacing = 8
contentStackView.axis = .vertical
for _ in 1...2 {
contentStackView.addArrangedSubview(makeConfigurationButton())
}
let button = UIButton(type: .system)
button.setTitle("Toggle", for: .normal)
button.addAction(buttonAction, for: .primaryActionTriggered)
view.addSubview(contentStackView)
view.addSubview(button)
contentStackView.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentStackView.topAnchor.constraint(equalTo: view.topAnchor),
contentStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
self.contentStackView = contentStackView
}
private var buttonAction: UIAction {
UIAction { [weak self] _ in
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0) {
guard let toggleElement = self?.contentStackView.arrangedSubviews[0] else { return }
toggleElement.isHidden.toggle()
toggleElement.alpha = toggleElement.isHidden ? 0 : 1
self?.contentStackView.layoutIfNeeded()
}
}
}
private func makeSystemButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitle("System Button", for: .normal)
return button
}
private func makeConfigurationButton() -> UIButton {
let button = UIButton()
var config = UIButton.Configuration.filled()
config.title = "Configuration Button"
button.configuration = config
return button
}
}
PlaygroundPage.current.liveView = MyViewController()
Which results in this animation:
But I want the animation to look like this, where the button only shrinks vertically:
Which you can replicate in the playground by just swapping contentStackView.addArrangedSubview(makeConfigurationButton()) for contentStackView.addArrangedSubview(makeSystemButton()).
I guess this has something to do with the stack view alignment, setting it to center gives me the desired animation, but then the buttons don't fill the stack view width anymore and setting the width through AutoLayout results in the same animation again... Also, having just one system button in the stack view results in the same weird animation, but why does it behave differently for two system buttons? What would be a good solution for this problem?
As you've seen, the built-in show/hide animation with UIStackView can be quirky (lots of other quirks when you really get into it).
It appears that, when using a button with UIButton.Configuration, the button's width changes from the width assigned by the stack view to its intrinsic width as the animation occurs.
We can get around that by giving the button an explicit height constraint -- but, what if we want to use the intrinsic height (which may not be known in advance)?
Instead of setting the constraint, set the button's Content Compression Resistance Priority::
button.configuration = config
// add this line
button.setContentCompressionResistancePriority(.required, for: .vertical)
return button
And we no longer get the horizontal sizing:
As you will notice, though, the button doesn't "squeeze" vertically... it gets "pushed up" outside the stack view's bounds.
We can avoid that by setting .clipsToBounds = true on the stack view:
If this effect is satisfactory, we're all set.
However, as we can see, the button is still not getting "squeezed." If that is the visual effect we want, we can use a custom "self-stylized" button instead of a Configuration button:
Of course, there is very little visual difference - and looking closely the button's text is not squeezing. If we really, really, really want that to happen, we need to animate a transform instead of using the stack view's default animation.
And... if we are taking advantage of some of the other conveniences with Configurations, using a self-stylized UIButton might not be an option.
If you want to play with the differences, here's some sample code:
class ViewController : UIViewController {
var btnStacks: [UIStackView] = []
override func viewDidLoad() {
view.backgroundColor = .systemYellow
let outerStack = UIStackView()
outerStack.axis = .vertical
outerStack.spacing = 12
for i in 1...3 {
let cv = UIView()
cv.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
let label = UILabel()
label.backgroundColor = .yellow
label.font = .systemFont(ofSize: 15, weight: .light)
let st = UIStackView()
st.axis = .vertical
st.spacing = 8
if i == 1 {
label.text = "Original Configuration Buttons"
for _ in 1...2 {
st.addArrangedSubview(makeOrigConfigurationButton())
}
}
if i == 2 {
label.text = "Resist Compression Configuration Buttons"
for _ in 1...2 {
st.addArrangedSubview(makeConfigurationButton())
}
}
if i == 3 {
label.text = "Custom Buttons"
for _ in 1...2 {
st.addArrangedSubview(makeCustomButton())
}
}
st.translatesAutoresizingMaskIntoConstraints = false
cv.addSubview(st)
NSLayoutConstraint.activate([
label.heightAnchor.constraint(equalToConstant: 28.0),
st.topAnchor.constraint(equalTo: cv.topAnchor),
st.leadingAnchor.constraint(equalTo: cv.leadingAnchor),
st.trailingAnchor.constraint(equalTo: cv.trailingAnchor),
cv.heightAnchor.constraint(equalToConstant: 100.0),
])
btnStacks.append(st)
outerStack.addArrangedSubview(label)
outerStack.addArrangedSubview(cv)
outerStack.setCustomSpacing(2.0, after: label)
}
// a horizontal stack view to hold a label and UISwitch
let ctbStack = UIStackView()
ctbStack.axis = .horizontal
ctbStack.spacing = 8
let label = UILabel()
label.text = "Clips to Bounds"
let ctbSwitch = UISwitch()
ctbSwitch.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)
ctbStack.addArrangedSubview(label)
ctbStack.addArrangedSubview(ctbSwitch)
// put the label/switch stack in a view so we can center it
let ctbView = UIView()
ctbStack.translatesAutoresizingMaskIntoConstraints = false
ctbView.addSubview(ctbStack)
// button to toggle isHidden/alpha on the first
// button in each stack view
let button = UIButton(type: .system)
button.setTitle("Toggle", for: .normal)
button.backgroundColor = .white
button.addAction(buttonAction, for: .primaryActionTriggered)
outerStack.addArrangedSubview(ctbView)
outerStack.addArrangedSubview(button)
outerStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(outerStack)
// respect safe-area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
outerStack.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
outerStack.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
outerStack.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
ctbStack.topAnchor.constraint(equalTo: ctbView.topAnchor),
ctbStack.bottomAnchor.constraint(equalTo: ctbView.bottomAnchor),
ctbStack.centerXAnchor.constraint(equalTo: ctbView.centerXAnchor),
])
}
#objc func switchChanged(_ sender: UISwitch) {
btnStacks.forEach { v in
v.clipsToBounds = sender.isOn
}
}
private var buttonAction: UIAction {
UIAction { [weak self] _ in
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0, delay: 0) {
guard let self = self else { return }
self.btnStacks.forEach { st in
st.arrangedSubviews[0].isHidden.toggle()
st.arrangedSubviews[0].alpha = st.arrangedSubviews[0].isHidden ? 0 : 1
}
}
}
}
private func makeOrigConfigurationButton() -> UIButton {
let button = UIButton()
var config = UIButton.Configuration.filled()
config.title = "Configuration Button"
button.configuration = config
return button
}
private func makeConfigurationButton() -> UIButton {
let button = UIButton()
var config = UIButton.Configuration.filled()
config.title = "Configuration Button"
button.configuration = config
// add this line
button.setContentCompressionResistancePriority(.required, for: .vertical)
return button
}
private func makeCustomButton() -> UIButton {
let button = UIButton()
button.setTitle("Custom Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.lightGray, for: .highlighted)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 6
return button
}
}
Looks like this:
Edit
Quick example of another "quirk" when it comes to hiding a stack view's arranged subview (excess code in here, but I stripped down the above example):
class MyViewController : UIViewController {
var btnStacks: [UIStackView] = []
override func viewDidLoad() {
view.backgroundColor = .systemYellow
let outerStack = UIStackView()
outerStack.axis = .vertical
outerStack.spacing = 12
let cv = UIView()
cv.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
let label = UILabel()
label.backgroundColor = .yellow
label.font = .systemFont(ofSize: 15, weight: .light)
let st = UIStackView()
st.axis = .vertical
st.spacing = 8
let colors: [UIColor] = [
.cyan, .green, .yellow, .orange, .white
]
label.text = "Labels"
for j in 0..<colors.count {
let v = UILabel()
v.text = "Label"
v.textAlignment = .center
v.backgroundColor = colors[j]
if j == 2 {
v.text = "Height Constraint = 80.0"
v.heightAnchor.constraint(equalToConstant: 80.0).isActive = true
}
st.addArrangedSubview(v)
}
st.translatesAutoresizingMaskIntoConstraints = false
cv.addSubview(st)
NSLayoutConstraint.activate([
label.heightAnchor.constraint(equalToConstant: 28.0),
st.topAnchor.constraint(equalTo: cv.topAnchor),
st.leadingAnchor.constraint(equalTo: cv.leadingAnchor),
st.trailingAnchor.constraint(equalTo: cv.trailingAnchor),
cv.heightAnchor.constraint(equalToConstant: 300.0),
])
btnStacks.append(st)
outerStack.addArrangedSubview(label)
outerStack.addArrangedSubview(cv)
outerStack.setCustomSpacing(2.0, after: label)
// button to toggle isHidden/alpha on the first
// button in each stack view
let button = UIButton(type: .system)
button.setTitle("Toggle", for: .normal)
button.backgroundColor = .white
button.addAction(buttonAction, for: .primaryActionTriggered)
outerStack.addArrangedSubview(button)
outerStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(outerStack)
// respect safe-area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
outerStack.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
outerStack.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
outerStack.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
])
}
private var buttonAction: UIAction {
UIAction { [weak self] _ in
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0, delay: 0) {
guard let self = self else { return }
self.btnStacks.forEach { st in
st.arrangedSubviews[2].isHidden.toggle()
}
}
}
}
}
When this is run and the "Toggle" button is tapped, it will be painfully obvious what's "not-quite-right."
You should add height constraint to buttons and update this constraint while animating. I edit your code just as below.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
private weak var contentStackView: UIStackView!
override func viewDidLoad() {
view.frame = CGRect(x: 0, y: 0, width: 300, height: 150)
view.backgroundColor = .white
let contentStackView = UIStackView()
contentStackView.spacing = 8
contentStackView.axis = .vertical
for _ in 1...2 {
contentStackView.addArrangedSubview(makeConfigurationButton())
}
let button = UIButton(type: .system)
button.setTitle("Toggle", for: .normal)
button.addAction(buttonAction, for: .primaryActionTriggered)
view.addSubview(contentStackView)
view.addSubview(button)
contentStackView.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentStackView.topAnchor.constraint(equalTo: view.topAnchor),
contentStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
self.contentStackView = contentStackView
}
private var buttonAction: UIAction {
UIAction { [weak self] _ in
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0) {
guard let toggleElement = self?.contentStackView.arrangedSubviews[0] else { return }
toggleElement.isHidden.toggle()
toggleElement.alpha = toggleElement.isHidden ? 0 : 1
toggleElement.heightAnchor.constraint(equalToConstant: toggleElement.isHidden ? 0 : 50)
self?.contentStackView.layoutIfNeeded()
}
}
}
private func makeSystemButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitle("System Button", for: .normal)
return button
}
private func makeConfigurationButton() -> UIButton {
let button = UIButton()
var config = UIButton.Configuration.filled()
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.heightAnchor.constraint(equalToConstant: 50)
])
config.title = "Configuration Button"
button.configuration = config
return button
}
}
PlaygroundPage.current.liveView = MyViewController()

Proper positioning of UIToolBar at the bottom of the screen

Due to lack of practice with creating UIToolBar element, I don't fully understand what is the cleanest way to position UIToolBar at the bottom of the screen. Now my code looks like this:
private lazy var toolBar: UIToolbar = {
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
toolBar.translatesAutoresizingMaskIntoConstraints = false
let addNewNote = UIBarButtonItem(barButtonSystemItem: .compose, target: toolBar, action: nil)
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: toolBar, action: nil)
toolBar.items = [spacer, addNewNote]
return toolBar
}()
private func setUpConstraints() {
NSLayoutConstraint.activate([
toolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
toolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
toolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
}
This gives desired results, however I feel like I am missing a better way to implement this simple element. First of all, once the device is rotated, ToolBar doesn't fill all the space from leading to trailing, secondly assigning both leading/trailing constraints and width/height based on UIScreen.bounds property just doesn't seem right, although previously I saw a constraints layout issue of ToolBarElement in the console before I assigned this strange frame to my ToolBar. Now this issue is gone, but the situation did not become clearer.
My goal is to clone UIToolBar from iOS native notes app
So, as an experiences iOS developer (of course you are!) what would you recommend me to do?
Have a nice day!
If I understand well you can do it with autolayaout:
Declare your objects under your controller class:
let toolBar: UIToolbar = {
//let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) // remove this
let toolBar = UIToolbar()
toolBar.translatesAutoresizingMaskIntoConstraints = false
let addNewNote = UIBarButtonItem(barButtonSystemItem: .compose, target: toolBar, action: nil)
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: toolBar, action: nil)
toolBar.tintColor = .yourColor
let toolBarTitleLabel = UILabel()
toolBarTitleLabel.text = "150 Results"
toolBarTitleLabel.sizeToFit()
toolBarTitleLabel.backgroundColor = .clear
toolBarTitleLabel.textColor = .white
toolBarTitleLabel.textAlignment = .center
let topBarButtonItemTitleLabel = UIBarButtonItem(customView: toolBarTitleLabel)
toolBar.items = [spacer, topBarButtonItemTitleLabel, spacer, addNewNote]
return toolBar
}()
let mylabel2: UILabel = {
let label = UILabel()
label.text = "Your results data text here..."
label.textAlignment = .center
label.textColor = .gray
label.font = .systemFont(ofSize: 16, weight: .regular)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let dummyView: UIView = {
let v = UIView()
v.backgroundColor = .systemBlue
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
In viewDidLoad set constraints:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
view.addSubview(toolBar)
NSLayoutConstraint.activate([
toolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
toolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
toolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
view.addSubview(mylabel2)
NSLayoutConstraint.activate([
mylabel2.bottomAnchor.constraint(equalTo: toolBar.topAnchor),
mylabel2.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30),
mylabel2.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30),
mylabel2.heightAnchor.constraint(equalToConstant: 50)
])
view.addSubview(dummyView)
NSLayoutConstraint.activate([
dummyView.bottomAnchor.constraint(equalTo: mylabel2.topAnchor),
dummyView.leadingAnchor.constraint(equalTo: mylabel2.leadingAnchor),
dummyView.trailingAnchor.constraint(equalTo: mylabel2.trailingAnchor),
dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30)
])
}
This is the result:

expand UIView height bottom to left and vice versa

I have a view that has a button on top and a textview on bottom as subviews, Im trying to have the button either expand and show the textview or collapse and hide it (like a show/hide)
I used the solution here and it kind of helped, except the subview (text view) was still showing and overlapping with other views so it only hid the main UIView.
here I initialized the height constraint to 25 to leave space for the button:
heightConstraint = detailView.heightAnchor.constraint(equalToConstant: 25)
the action function that'll expand/collapse the view based on the height constraint
#objc func expandViewPressed(sender: UIButton) {
if isAnimating { return }
let shouldCollapse = detailView.frame.height > 25
animateView(isCollapsed: shouldCollapse)
}
the animation function
private func animateView(isCollapsed: Bool) {
heightConstraint[enter image description here][1].isActive = isCollapsed
isAnimating = true
UIView.animate(withDuration: 1, animations: {
self.detailText.isHidden = isCollapsed
self.view.layoutIfNeeded()
}) { (_) in
self.isAnimating = false
}
}
—
expanded view
Sherbini .. please make sure you have added proper constraints to your subviews .. make sure not to add height constraint on any of your subview ..
also make sure you have added view.clipsToBounds == true
Hope it will work for you ..
There are various ways to approach this.
One method is to use two "bottom" constraints:
one from the bottom of the button to the bottom of detailView
one from the bottom of detailText to the bottom of detailView
Then set the constraint Priority of each based on whether the view should be "collapsed" or "expanded."
Here is a full implementation for you to try:
class ExpandViewController: UIViewController {
let myButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("Collapse", for: [])
v.backgroundColor = .red
return v
}()
let detailText: UITextView = {
let v = UITextView()
v.translatesAutoresizingMaskIntoConstraints = false
v.text = "This is text in the text view."
return v
}()
let detailView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor(red: 0.25, green: 0.5, blue: 1.0, alpha: 1.0)
v.clipsToBounds = true
return v
}()
var isAnimating: Bool = false
var collapsedConstraint: NSLayoutConstraint!
var expandedConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(red: 0.0, green: 0.75, blue: 0.0, alpha: 1.0)
detailView.addSubview(myButton)
detailView.addSubview(detailText)
view.addSubview(detailView)
let g = view.safeAreaLayoutGuide
// when collapsed, we want button bottom to constrain detailView bottom
collapsedConstraint = myButton.bottomAnchor.constraint(equalTo: detailView.bottomAnchor, constant: -12.0)
// when expanded, we want textView bottom to constrain detailView bottom
expandedConstraint = detailText.bottomAnchor.constraint(equalTo: detailView.bottomAnchor, constant: -12.0)
// we'll start in Expanded state
expandedConstraint.priority = .defaultHigh
collapsedConstraint.priority = .defaultLow
NSLayoutConstraint.activate([
// constrain detailView Top / Leading / Trailing
detailView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
detailView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
detailView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
// no Height or Bottom constraint for detailView
// constrain button Top / Center / Width
myButton.topAnchor.constraint(equalTo: detailView.topAnchor, constant: 12.0),
myButton.centerXAnchor.constraint(equalTo: detailView.centerXAnchor),
myButton.widthAnchor.constraint(equalToConstant: 200.0),
// constrain detailText Top / Leading / Trailing
detailText.topAnchor.constraint(equalTo: myButton.bottomAnchor, constant: 12.0),
detailText.leadingAnchor.constraint(equalTo: detailView.leadingAnchor, constant: 12.0),
detailText.trailingAnchor.constraint(equalTo: detailView.trailingAnchor, constant: -12.0),
// constrain detailText's Height
detailText.heightAnchor.constraint(equalToConstant: 200.0),
expandedConstraint,
collapsedConstraint,
])
myButton.addTarget(self, action: #selector(self.expandViewPressed(sender:)), for: .touchUpInside)
}
#objc func expandViewPressed(sender: UIButton) {
if isAnimating { return }
animateView()
}
private func animateView() {
isAnimating = true
// if it's expanded
if expandedConstraint.priority == .defaultHigh {
expandedConstraint.priority = .defaultLow
collapsedConstraint.priority = .defaultHigh
} else {
collapsedConstraint.priority = .defaultLow
expandedConstraint.priority = .defaultHigh
detailText.isHidden = false
}
UIView.animate(withDuration: 1, animations: {
self.view.layoutIfNeeded()
}) { (_) in
self.detailText.isHidden = self.expandedConstraint.priority == .defaultLow
self.isAnimating = false
self.myButton.setTitle(self.detailText.isHidden ? "Expand" : "Collapse", for: [])
}
}
}

UIStackView setting layoutMargins in code breaks alignment

I'm trying to add layoutMargins to some elements in a UIStackView I'm creating in code.
I've tried the same thing in IB and it works perfectly, but when I add layoutMargins and I set isLayoutMarginsRelativeArrangement = true then my stack view's alignment breaks.
The code of my stack view is the following:
#objc lazy var buttonsStackView: UIStackView = {
let stack = UIStackView(arrangedSubviews: [doneButton, separatorView, giftButton])
stack.layoutMargins = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4)
stack.isLayoutMarginsRelativeArrangement = true
stack.axis = .horizontal
stack.frame = CGRect(x: 0, y: 0, width: 150, height: 44)
stack.spacing = 4
stack.distribution = .equalCentering
stack.alignment = .center
let bgView = UIView(frame: stack.bounds)
stackViewBackground = bgView
bgView.backgroundColor = ColorManager.shared.grayColor
bgView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
stack.insertSubview(bgView, at: 0)
separatorView.heightAnchor.constraint(equalTo: stack.heightAnchor, multiplier: 0.8).isActive = true
return stack
}()
SeparatorView only has a 1pt width constraint, while the two buttons are left unconstrained to keep theirintrinsicContentSize.
Here's how my stackView looks when isLayoutMarginsRelativeArrangement is false:
But obviously, the left and right margins are needed, so when setting isLayoutMarginsRelativeArrangement to true, my stackview's alignment breaks:
Unfortunately, I cannot use IB for this particular view and I need to initialise it from code. Any idea on how to fix this is greatly appreciated. Thank you!
Here is an example of making that a custom view, with the separator centered horizontally, and the buttons centered in each "side":
protocol DoneGiftDelegate: class {
func doneButtonTapped()
func giftButtonTapped()
}
class DoneGiftView: UIView {
weak var delegate: DoneGiftDelegate?
let doneButton: UIButton = {
let v = UIButton(type: .system)
v.setTitle("Done", for: [])
v.tintColor = .white
return v
}()
let giftButton: UIButton = {
let v = UIButton(type: .system)
v.setImage(UIImage(systemName: "gift.fill"), for: [])
v.tintColor = .white
return v
}()
let separatorView: UIView = {
let v = UIView()
v.backgroundColor = .white
return v
}()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
backgroundColor = .lightGray // ColorManager.shared.grayColor
[doneButton, separatorView, giftButton].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
addSubview($0)
}
// width and height constraints: 150 x 44
// set Priority to 999 so it can be overriden by controller if desired
let widthConstraint = widthAnchor.constraint(equalToConstant: 150.0)
widthConstraint.priority = UILayoutPriority(rawValue: 999)
let heightConstraint = heightAnchor.constraint(equalToConstant: 44.0)
heightConstraint.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
// doneButton Leading to Leading
doneButton.leadingAnchor.constraint(equalTo: leadingAnchor),
// separator Leading to doneButton Trailing
separatorView.leadingAnchor.constraint(equalTo: doneButton.trailingAnchor),
// giftButton Leading to separator Trailing
giftButton.leadingAnchor.constraint(equalTo: separatorView.trailingAnchor),
// giftButton Trailing to Trailing
giftButton.trailingAnchor.constraint(equalTo: trailingAnchor),
// all centered vertically
doneButton.centerYAnchor.constraint(equalTo: centerYAnchor),
separatorView.centerYAnchor.constraint(equalTo: centerYAnchor),
giftButton.centerYAnchor.constraint(equalTo: centerYAnchor),
// doneButton Height = Height
doneButton.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1.0),
// separator Height = 80% of Height
separatorView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.8),
// giftButton Height = Height
giftButton.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1.0),
// separator Width = 1
separatorView.widthAnchor.constraint(equalToConstant: 1.0),
// doneButton Width = giftButton Width
doneButton.widthAnchor.constraint(equalTo: giftButton.widthAnchor, multiplier: 1.0),
// self Width and Height
widthConstraint,
heightConstraint,
])
// add target actions for buttons
doneButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
giftButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}
#objc func buttonTapped(_ sender: UIButton) -> Void {
if sender == doneButton {
delegate?.doneButtonTapped()
} else {
delegate?.giftButtonTapped()
}
}
}
class DemoViewController: UIViewController, DoneGiftDelegate {
let doneGiftView: DoneGiftView = DoneGiftView()
let testView: UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
doneGiftView.translatesAutoresizingMaskIntoConstraints = false
testView.translatesAutoresizingMaskIntoConstraints = false
testView.addSubview(doneGiftView)
view.addSubview(testView)
// so we can see the view frame
testView.backgroundColor = .cyan
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// testView Top: 100
// Leading / Trailing with 20-pts "padding"
// Height: 80
testView.topAnchor.constraint(equalTo: g.topAnchor, constant: 100.0),
testView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
testView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
testView.heightAnchor.constraint(equalToConstant: 80.0),
// doneGiftView Trailing to testView Trailing
doneGiftView.trailingAnchor.constraint(equalTo: testView.trailingAnchor),
// doneGiftView centered vertically in testView
doneGiftView.centerYAnchor.constraint(equalTo: testView.centerYAnchor),
])
doneGiftView.delegate = self
}
func doneButtonTapped() {
print("Done button tapped!")
// do what we want when Done button tapped
}
func giftButtonTapped() {
print("Gift button tapped")
// do what we want when Gift button tapped
}
}
Example result:

iOS Ensure Bar Button Items Appear Full-Size in UINavigationBar on iPhone SE

In my app, I have a navigation bar where I'm using three buttons on the right side. I create it like this:
let button1 = UIBarButtonItem(image: UIImage(named: "button1"),
style: .plain,
target: self,
action: #selector(self.button1Tapped))
// Repeat for button2 and button3
navigationItem.rightBarButtonItems = [button1, button2, button3]
This works as expected, except on iPhone SE. It seems like there is some sort of fixed limit on what percentage of the navigation bar width the right bar buttons can take up, and this limit is exceeded on the small screen of the iPhone SE. So, the rightmost button gets shrunk down to about half the size of the other buttons.
I'm not setting a title, so there's more than enough space for all 3 buttons to be full size - even on iPhone SE. However, I'm not sure how to specify that in code; is there some way to increase this limit (or whatever feature causes the button to shrink) to ensure that all of the BarButtonItems appear full size on iPhone SE?
This isn't the most eloquent way of doing things but it should work.
The iPhone SE screen's dimensions are w:320 x h:568 Apple Screen Display Sizes
You could possibly resize the images for the iPhone SE and do something like this:
var button1: UIBarButtonItem!
var button2: UIBarButtonItem!
var button2: UIBarButtonItem!
// check the screen's dimensions
if UIScreen.main.bounds.width == 320 && UIScreen.main.bounds.height == 568{
// you may have to play around with it but resize the UIImage(named: "button1") to fit the iPhone SE
button1 = UIBarButtonItem(image: UIImage(named: "button1_Resized"),
style: .plain,
target: self,
action: #selector(self.button1Tapped))
// Repeat for button2 and button3
// resize the UIImage(named: "button2_Resized") and UIImage(named: "button3_Resized") to fit the iPhone SE
} else{
// anything other then the iPhone SE will get the regular sized images
button1 = UIBarButtonItem(image: UIImage(named: "button1"),
style: .plain,
target: self,
action: #selector(self.button1Tapped))
// Repeat for button2 and button3
}
navigationItem.rightBarButtonItems = [button1, button2, button3]
You can declare these properties
let contentView = UIView()
let buttonOneImageView = UIImageView()
let buttonTwoImageView = UIImageView()
let buttonThreeImageView = UIImageView()
assign a UIView to titleView property of navigationItem of ViewController as a workaround
fileprivate func buttonOneImageViewConstraints() {
buttonOneImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
buttonOneImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
buttonOneImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8).isActive = true
buttonOneImageView.widthAnchor.constraint(equalTo: buttonOneImageView.heightAnchor).isActive = true
}
fileprivate func buttonOneConstraints(_ button1: UIButton) {
//button1 constraints
button1.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
button1.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
button1.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8).isActive = true
button1.widthAnchor.constraint(equalTo: button1.heightAnchor).isActive = true
}
fileprivate func buttonTwoImageViewConstraints() {
buttonTwoImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
buttonTwoImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
buttonTwoImageView.rightAnchor.constraint(equalTo: buttonOneImageView.leftAnchor, constant: -8).isActive = true
buttonTwoImageView.widthAnchor.constraint(equalTo: buttonTwoImageView.heightAnchor).isActive = true
}
fileprivate func buttonTwoConstraints(_ button1: UIButton,_ button2: UIButton) {
//button2 constraints
button2.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
button2.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
button2.rightAnchor.constraint(equalTo: button1.leftAnchor, constant: -8).isActive = true
button2.widthAnchor.constraint(equalTo: button2.heightAnchor).isActive = true
}
fileprivate func buttonThreeImageViewConstraints() {
buttonThreeImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
buttonThreeImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
buttonThreeImageView.rightAnchor.constraint(equalTo: buttonTwoImageView.leftAnchor, constant: -8).isActive = true
buttonThreeImageView.widthAnchor.constraint(equalTo: buttonThreeImageView.heightAnchor).isActive = true
}
fileprivate func buttonThreeConstraints(_ button2: UIButton,_ button3: UIButton) {
//button3 constraints
button3.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
button3.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
button3.rightAnchor.constraint(equalTo: button2.leftAnchor, constant: -8).isActive = true
button3.widthAnchor.constraint(equalTo: button3.heightAnchor).isActive = true
}
fileprivate func contentViewConstraints(_ titleView: UIView) {
//setting constraints for contentView
contentView.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = true
contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
contentView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
contentView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
self.navigationItem.titleView = titleView
}
func setupNavBar() {
contentView.backgroundColor = .black
contentView.translatesAutoresizingMaskIntoConstraints = false
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
titleView.addSubview(contentView)
buttonOneImageView.translatesAutoresizingMaskIntoConstraints = false
buttonOneImageView.contentMode = .scaleAspectFit
buttonOneImageView.image = #imageLiteral(resourceName: "contacts-settings")
contentView.addSubview(buttonOneImageView)
//add button1
let button1 = UIButton(type: .roundedRect)
button1.translatesAutoresizingMaskIntoConstraints = false
button1.addTarget(self, action: #selector(buttonOneFunction), for: .touchUpInside)
contentView.addSubview(button1)
buttonTwoImageView.translatesAutoresizingMaskIntoConstraints = false
buttonTwoImageView.contentMode = .scaleAspectFit
buttonTwoImageView.image = #imageLiteral(resourceName: "contacts-history")
contentView.addSubview(buttonTwoImageView)
//add button2
let button2 = UIButton(type: .roundedRect)
button2.translatesAutoresizingMaskIntoConstraints = false
button2.addTarget(self, action: #selector(buttonTwoFunction), for: .touchUpInside)
contentView.addSubview(button2)
buttonThreeImageView.translatesAutoresizingMaskIntoConstraints = false
buttonThreeImageView.contentMode = .scaleAspectFit
buttonThreeImageView.image = #imageLiteral(resourceName: "contacts-person")
contentView.addSubview(buttonThreeImageView)
//add button3
let button3 = UIButton(type: .roundedRect)
button3.translatesAutoresizingMaskIntoConstraints = false
button3.addTarget(self, action: #selector(buttonThreeFunction), for: .touchUpInside)
contentView.addSubview(button3)
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.buttonOneImageViewConstraints()
self.buttonOneConstraints(button1)
self.buttonTwoImageViewConstraints()
self.buttonTwoConstraints(button1, button2)
self.buttonThreeImageViewConstraints()
self.buttonThreeConstraints(button2, button3)
self.contentViewConstraints(titleView)
}
}
Similarly,
#objc func buttonTwoFunction() {
print("button 2 tapped")
}
#objc func buttonThreeFunction() {
print("button 3 tapped")
}

Resources