Setting up UI constraint (Image and stackView inside a UIView) programmatically in Swift - ios

I'm trying to build a custom AD when the app opens it pop up some UIViews and image and two buttons then control it from my Firebase, for now I have problem adding the adImage and buttonsStack(contains 2 buttons) inside my backView programmatically and so far nothing works ..
I need the image takes ~ %75 of the backView up and the buttonsStack ~ %25 of the rest
here some code and I have upload it to my GitHub
import UIKit
class ViewController: UIViewController {
let backroundView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .black
view.alpha = 0.5
return view
}()
let backView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
view.layer.cornerRadius = 15
return view
}()
let adImage: UIImageView = {
var image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFill
return image
}()
let buttonsStack: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.alignment = UIStackViewAlignment.fill
stack.axis = UILayoutConstraintAxis.vertical
stack.distribution = .equalSpacing
stack.spacing = 8
stack.backgroundColor = UIColor.red
return stack
}()
let actionButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Open", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(red: 0, green: 0.60, blue: 1, alpha: 1)
button.layer.cornerRadius = 8
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.textAlignment = .center
return button
}()
let dismessButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Exit", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .lightGray
button.layer.cornerRadius = 8
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.textAlignment = .center
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI(){
// backroundView
view.addSubview(backroundView)
backroundView.frame = view.frame
// backView
view.addSubview(backView)
backView.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
backView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
backView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
backView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
// adImage
backView.addSubview(adImage)
adImage.image = UIImage(named: "testImage")
adImage.topAnchor.constraint(equalTo: backView.topAnchor).isActive = true
adImage.rightAnchor.constraint(equalTo: backView.rightAnchor).isActive = true
adImage.leftAnchor.constraint(equalTo: backView.leftAnchor).isActive = true
adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.50).isActive = true
// buttonsStack
buttonsStack.addArrangedSubview(actionButton)
buttonsStack.addArrangedSubview(dismessButton)
backView.addSubview(buttonsStack)
buttonsStack.topAnchor.constraint(equalTo: backView.topAnchor, constant: 15).isActive = true
buttonsStack.bottomAnchor.constraint(equalTo: backView.bottomAnchor, constant: -15).isActive = true
buttonsStack.rightAnchor.constraint(equalTo: backView.rightAnchor, constant: -15).isActive = true
buttonsStack.leftAnchor.constraint(equalTo: backView.leftAnchor, constant: 15).isActive = true
}
}

For the image to take 0.75 change this
adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.50).isActive = true
to
adImage.heightAnchor.constraint(equalTo: backView.heightAnchor, multiplier: 0.75).isActive = true
//
then the buttonStack should goes under it so change this
buttonsStack.topAnchor.constraint(equalTo: backView.topAnchor, constant: 15).isActive = true
to
buttonsStack.topAnchor.constraint(equalTo: adImage.bottomAnchor, constant: 15).isActive = true

Related

Setting UITextField heightAnchor to 0 does not work on iOS 15

I have an iOS application where i have a textfield and a button and on tap of button i have to hide the textfield.
I am setting heightAnchor to 0 on tap of button. Everything is working fine on iOS 14(14.5) but does not work(does not hide the text field) on iOS 15. Also, I have tried setting up the isHidden property on UITextField but it does not work.
Can you please help tell if something changed or i am doing something wrong. Thank you.
Code reference:
import UIKit
class ViewController: UIViewController {
private lazy var mytextFeild: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.text = "Hello world"
textField.backgroundColor = .green
return textField
}()
private lazy var testView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemPink
return view
}()
private lazy var button: UIButton = {
let view = UIButton()
view.backgroundColor = .blue
view.setTitle("hide it", for: .normal)
view.translatesAutoresizingMaskIntoConstraints = false
view.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return view
}()
var heightConstraint: NSLayoutConstraint?
#objc func buttonTapped() {
heightConstraint?.isActive = false
heightConstraint = mytextFeild.heightAnchor.constraint(equalToConstant: 0)
heightConstraint?.isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(mytextFeild)
view.addSubview(testView)
view.addSubview(button)
mytextFeild.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
mytextFeild.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32.0).isActive = true
mytextFeild.topAnchor.constraint(equalTo: view.topAnchor, constant: 64).isActive = true
heightConstraint = mytextFeild.heightAnchor.constraint(equalToConstant: 32.0)
heightConstraint?.isActive = true
button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32.0).isActive = true
button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64.0).isActive = true
testView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
testView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
testView.topAnchor.constraint(equalTo: mytextFeild.bottomAnchor).isActive = true
testView.bottomAnchor.constraint(equalTo: button.topAnchor).isActive = true
}
}
Add them to a stack then add stack to the viewController. at the end try to hide it easily without changing the height.
class ViewController: UIViewController {
private lazy var stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
private lazy var myTextField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.text = "Hello world"
textField.backgroundColor = .green
return textField
}()
private lazy var testView: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemPink
return view
}()
private lazy var button: UIButton = {
let view = UIButton()
view.backgroundColor = .blue
view.setTitle("hide it", for: .normal)
view.translatesAutoresizingMaskIntoConstraints = false
view.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return view
}()
#objc func buttonTapped() {
myTextField.isHidden = !myTextField.isHidden
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
stackView.addArrangedSubview(myTextField)
stackView.addArrangedSubview(testView)
stackView.addArrangedSubview(button)
view.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -64.0).isActive = true
stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
myTextField.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
myTextField.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -64.0).isActive = true
testView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
button.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -64.0).isActive = true
}
}

UIScrollView with UIView Can't Select Any Subview Items

I currently have a UIViewController setup like this, and I'm unable to interact with the controls in the contentView.
I'm currently following this concept: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html
class SimpleViewController: UIViewController {
override func viewDidLoad() {
let scrollView: UIScrollView = {
let newScrollView = UIScrollView()
newScrollView.translatesAutoresizingMaskIntoConstraints = false
let safeGuide = view.safeAreaLayoutGuide
self.view.addSubview(newScrollView)
newScrollView.topAnchor.constraint(equalTo: safeGuide.topAnchor).isActive = true
newScrollView.leadingAnchor.constraint(equalTo: safeGuide.leadingAnchor).isActive = true
newScrollView.trailingAnchor.constraint(equalTo: safeGuide.trailingAnchor).isActive = true
newScrollView.bottomAnchor.constraint(equalTo: safeGuide.bottomAnchor).isActive = true
return newScrollView
}()
let contentView: UIView = {
let newView = UIView()
newView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(newView)
newView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
newView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
newView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0).isActive = true
newView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
return newView
}()
let createBtn: UIButton = {
let btn = UIButton()
btn.setTitle("Create", for: UIControl.State.normal)
btn.backgroundColor = .orange;
btn.translatesAutoresizingMaskIntoConstraints = false;
contentView.addSubview(btn)
btn.layer.cornerRadius = 6.0;
btn.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10);
btn.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
btn.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
btn.widthAnchor.constraint(equalToConstant: 100).isActive = true
return btn
}()
let cancelBtn: UIButton = {
let btn = UIButton()
btn.setTitle("Cancel", for: UIControl.State.normal)
btn.backgroundColor = .red;
btn.translatesAutoresizingMaskIntoConstraints = false;
contentView.addSubview(btn)
btn.layer.cornerRadius = 6.0;
btn.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10);
btn.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
btn.leadingAnchor.constraint(equalTo: createBtn.trailingAnchor).isActive = true
btn.widthAnchor.constraint(equalToConstant: 100).isActive = true
btn.addTarget(self, action: #selector(self.cancelCreateOrder), for: .touchUpInside);
return btn
}()
}
}
If I were to add the buttons directly to the scrollView, then I can interact with them. However, with them in the contentView, it's not registering any clicks.
Add 2 constraints
// contentView should tell scrollView it's width ( setting the leading and trailing isn't enough what matters the width to be inside the scrollView which is still un-known even after hooking the leading & trailing )
newView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
And
// contentView should tell scrollView it's height by top & bottom constraints hooking
btn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

ViewController Layout Help Needed

I'm using XCode to build a UI for a countdown timer/metronome app. After struggling with layout constraints, I discovered vertical and horizontal stack views, which I thought would make things much easier.
However, the outermost stackview's boundaries appear to be outside the view of the device. I removed all the layout constraints, but that didn't make any difference.
Here's a partial screenshot of the project in XCode:
StackView boundaries
Sorry if this question has been asked before - I looked but haven't found anything.
I'm a professional software developer for the last 30 years or so. The languages I work with on a daily basis for the last 20 years or so are Java, C++, and Fortran. So dealing with UI layout in IOS is frustrating, to say the least!
Thanks in advance for any help!
Edit: Here's a screenshot of the uppermost stack view's size inspector: size inspector
I know your frustration well because it was mine too when I start, and I decided to give you a hand. Make all UI programmatically like this:
in your Controller class built your objects programmatically:
let buttonPlay: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.yourColor
let image = UIImage(systemName: "play.fill")
button.setImage(image, for: .normal)
button.tintColor = .black
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.layer.cornerRadius = 25
button.clipsToBounds = true
return button
}()
let buttonPause: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.yourColor
let image = UIImage(systemName: "pause.fill")
button.setImage(image, for: .normal)
button.tintColor = .black
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.layer.cornerRadius = 25
button.clipsToBounds = true
return button
}()
let buttonStop: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.yourColor
let image = UIImage(systemName: "stop.fill")
button.setImage(image, for: .normal)
button.tintColor = .black
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.layer.cornerRadius = 25
button.clipsToBounds = true
return button
}()
let minutesLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .white
label.text = "MM"
label.textColor = .gray
label.textAlignment = .center
label.font = .systemFont(ofSize: 20, weight: .semibold)
return label
}()
let secondsLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .white
label.text = "SS"
label.textColor = .gray
label.textAlignment = .center
label.font = .systemFont(ofSize: 20, weight: .semibold)
return label
}()
let separatorLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .clear
label.text = ":"
label.textColor = .black
label.textAlignment = .center
label.font = .systemFont(ofSize: 20, weight: .semibold)
return label
}()
let titleLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor.yourColor
label.text = "Title label"
label.textColor = .black
label.textAlignment = .center
label.font = .systemFont(ofSize: 20, weight: .semibold)
label.translatesAutoresizingMaskIntoConstraints = false // this distactive automatic constraints
return label
}()
let timeLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .clear
label.text = "Tempo:"
label.textColor = .black
label.font = .systemFont(ofSize: 20, weight: .regular)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let imageViewTime: UIImageView = {
let image = UIImage(named: "yourImage")?.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image: image)
imageView.tintColor = .white
imageView.backgroundColor = .purple
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let slider: UISlider = {
let s = UISlider()
s.maximumValue = 0
s.maximumValue = 100
s.isContinuous = true
s.tintColor = .black
s.addTarget(self, action: #selector(sliderInAction), for: .valueChanged) // call the function below to show slider value
s.translatesAutoresizingMaskIntoConstraints = false
return s
}()
let sliderLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .clear
label.text = "2"
label.textAlignment = .center
label.textColor = .black
label.font = .systemFont(ofSize: 16, weight: .regular)
label.widthAnchor.constraint(equalToConstant: 30).isActive = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let containerSlider: UIView = {
let v = UIView()
v.backgroundColor = UIColor.yourColor
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
In viewDidLoad set your background color and call the function to setup the programmatically constraints:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.yourColor
setupConstraints()
}
now set the constraints with autolayout:
fileprivate func setupConstraints() {
let stackViewTime = UIStackView(arrangedSubviews: [minutesLabel, separatorLabel, secondsLabel])
stackViewTime.distribution = .fillProportionally
stackViewTime.spacing = 4
stackViewTime.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackViewTime)
stackViewTime.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
stackViewTime.heightAnchor.constraint(equalToConstant: 50).isActive = true
stackViewTime.widthAnchor.constraint(equalToConstant: 148).isActive = true
stackViewTime.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
let stackViewPlayer = UIStackView(arrangedSubviews: [buttonPlay, buttonPause, buttonStop])
stackViewPlayer.distribution = .fillEqually
stackViewPlayer.spacing = 10
stackViewPlayer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackViewPlayer)
stackViewPlayer.widthAnchor.constraint(equalToConstant: 170).isActive = true
stackViewPlayer.heightAnchor.constraint(equalToConstant: 50).isActive = true
stackViewPlayer.centerXAnchor.constraint(equalTo: stackViewTime.centerXAnchor).isActive = true
stackViewPlayer.topAnchor.constraint(equalTo: stackViewTime.bottomAnchor, constant: 20).isActive = true
view.addSubview(imageViewTime)
imageViewTime.topAnchor.constraint(equalTo: stackViewPlayer.bottomAnchor, constant: 20).isActive = true
imageViewTime.heightAnchor.constraint(equalToConstant: 80).isActive = true
imageViewTime.widthAnchor.constraint(equalToConstant: 60).isActive = true
imageViewTime.centerXAnchor.constraint(equalTo: stackViewTime.centerXAnchor).isActive = true
view.addSubview(titleLabel)
titleLabel.topAnchor.constraint(equalTo: imageViewTime.bottomAnchor, constant: 20).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
view.addSubview(timeLabel)
timeLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10).isActive = true
timeLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
timeLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
timeLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
let stackSlider = UIStackView(arrangedSubviews: [sliderLabel, slider])
stackSlider.distribution = .fill
stackSlider.spacing = 10
stackSlider.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerSlider)
containerSlider.topAnchor.constraint(equalTo: timeLabel.bottomAnchor, constant: 0).isActive = true
containerSlider.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
containerSlider.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
containerSlider.heightAnchor.constraint(equalToConstant: 30).isActive = true
containerSlider.addSubview(stackSlider)
stackSlider.topAnchor.constraint(equalTo: containerSlider.topAnchor).isActive = true
stackSlider.bottomAnchor.constraint(equalTo: containerSlider.bottomAnchor).isActive = true
stackSlider.leadingAnchor.constraint(equalTo: containerSlider.leadingAnchor, constant: 10).isActive = true
stackSlider.trailingAnchor.constraint(equalTo: containerSlider.trailingAnchor, constant: -10).isActive = true
}
the last step set the function to show the slider value in your slider label when you move it:
#objc fileprivate func sliderInAction() {
sliderLabel.text = "\(Int(slider.value))"
}
This is the result:
Play with this code, an experienced developer like you should take a short time to write the code.

Programmatic layouts showing error in landscape mode

Here is the screenshot if the errorI am building the ui of the app programatically and when i run the app it runs perfectly but when i turn the simulation in landscape mode the console shows some layouts errors. Is it okay to just tick the orientation to portrait only in the general app settings because my app is running perfectly in portrait mode and i don't want to run the app in any other orientations or am i making some problems in constraints? Here is my code for the ui...
`let top = UIColor(red: 217/255, green: 30/255, blue: 133/255, alpha: 1)
let bottom = UIColor(red: 242/255, green: 56/255, blue: 15/255, alpha: 1)
let colorOne = UIColor(red: 38/255, green: 38/255, blue: 38/255, alpha: 1)
let colorTwo = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1)
let colorTop = UIColor.red
let colorBottom = UIColor.green
let topContainer: UIView = {
let top = UIView()
top.translatesAutoresizingMaskIntoConstraints = false
top.backgroundColor = .black
return top
}()
let model: UIImageView = {
let mymodel = UIImageView(image: #imageLiteral(resourceName: "bodybuilder"))
mymodel.translatesAutoresizingMaskIntoConstraints = false
mymodel.contentMode = .scaleToFill
return mymodel
}()
let logo: UIImageView = {
let gymble = UIImageView(image: #imageLiteral(resourceName: "Gymble"))
gymble.contentMode = .scaleAspectFill
gymble.translatesAutoresizingMaskIntoConstraints = false
return gymble
}()
let bottomContainer: UIView = {
let bottom = UIView()
bottom.translatesAutoresizingMaskIntoConstraints = false
return bottom
}()
let pinField: UITextField = {
let phone = UITextField()
phone.backgroundColor = .white
phone.text = "+91"
phone.textAlignment = .center
phone.layer.cornerRadius = 5
phone.font = UIFont(name: "Roboto-Regular", size: 20)
phone.keyboardType = UIKeyboardType.numberPad
phone.translatesAutoresizingMaskIntoConstraints = false
return phone
}()
let phoneFeild: UITextField = {
let phone = UITextField()
phone.backgroundColor = .white
phone.placeholder = "Phone number"
phone.layer.cornerRadius = 5
phone.font = UIFont(name: "Roboto-Regular", size: 20)
phone.keyboardType = UIKeyboardType.phonePad
phone.keyboardAppearance = UIKeyboardAppearance.dark
phone.translatesAutoresizingMaskIntoConstraints = false
return phone
}()
let loginButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Login", for: .normal)
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 5
button.layer.masksToBounds = true
button.titleLabel?.font = UIFont(name: "Roboto-Medium", size: 22)
button.addTarget(self, action: #selector(someButtonAction), for: .touchUpInside)
return button
}()
let laterButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("I'll login later", for: .normal)
button.setTitleColor(.gray, for: .normal)
button.titleLabel?.font = UIFont(name: "Roboto-Light", size: 16)
return button
}()
let horizontalStack: UIStackView = {
let mystack = UIStackView()
mystack.alignment = UIStackView.Alignment.center
mystack.axis = NSLayoutConstraint.Axis.horizontal
mystack.translatesAutoresizingMaskIntoConstraints = false
mystack.distribution = .fill
mystack.spacing = 15
return mystack
}()
let stack: UIStackView = {
let mystack = UIStackView()
mystack.alignment = UIStackView.Alignment.center
mystack.axis = NSLayoutConstraint.Axis.vertical
mystack.translatesAutoresizingMaskIntoConstraints = false
mystack.distribution = .equalSpacing
return mystack
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
view.addSubview(topContainer)
topContainerLayout()
topContainer.addSubview(model)
modelLayout()
view.addSubview(bottomContainer)
bottomContainerLayout()
bottomContainer.addSubview(stack)
stackLayout()
stack.addArrangedSubview(logo)
logoLayout()
stack.addArrangedSubview(horizontalStack)
horizontalStackLayout()
horizontalStack.addArrangedSubview(pinField)
pinFieldLayout()
let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.phoneFeild.frame.height))
phoneFeild.leftView = padding
phoneFeild.leftViewMode = UITextField.ViewMode.always
horizontalStack.addArrangedSubview(phoneFeild)
phoneFieldLayout()
let gradientWidth = (UIScreen.main.bounds.width - 32)
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [top.cgColor, bottom.cgColor]
gradientLayer.locations = [0.15, 1]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 0)
gradientLayer.frame = CGRect(x: 0, y: 0, width: gradientWidth, height: 50)
gradientLayer.cornerRadius = 5
loginButton.layer.insertSublayer(gradientLayer, at: 0)
stack.addArrangedSubview(loginButton)
loginButtonLayout()
stack.addArrangedSubview(laterButton)
LaterButtonLayout()
}
func LaterButtonLayout(){
laterButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
func loginButtonLayout(){
loginButton.widthAnchor.constraint(equalTo: stack.widthAnchor, constant: -32).isActive = true
loginButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
func logoLayout(){
logo.topAnchor.constraint(equalTo: stack.topAnchor).isActive = true
logo.heightAnchor.constraint(equalToConstant: 56).isActive = true
logo.widthAnchor.constraint(equalToConstant: 60).isActive = true
}
func stackLayout(){
stack.heightAnchor.constraint(equalTo: bottomContainer.heightAnchor).isActive = true
stack.widthAnchor.constraint(equalTo: bottomContainer.widthAnchor).isActive = true
}
func horizontalStackLayout(){
horizontalStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
horizontalStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
horizontalStack.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
func pinFieldLayout(){
pinField.leadingAnchor.constraint(equalTo: horizontalStack.leadingAnchor).isActive = true
pinField.topAnchor.constraint(equalTo: horizontalStack.topAnchor).isActive = true
pinField.bottomAnchor.constraint(equalTo: horizontalStack.bottomAnchor).isActive = true
pinField.widthAnchor.constraint(equalToConstant: 50).isActive = true
}
func phoneFieldLayout(){
phoneFeild.leadingAnchor.constraint(equalTo: pinField.trailingAnchor, constant: 15).isActive = true
phoneFeild.topAnchor.constraint(equalTo: horizontalStack.topAnchor).isActive = true
phoneFeild.bottomAnchor.constraint(equalTo: horizontalStack.bottomAnchor).isActive = true
}
func modelLayout(){
model.topAnchor.constraint(equalTo: topContainer.topAnchor).isActive = true
model.leadingAnchor.constraint(equalTo: topContainer.leadingAnchor).isActive = true
model.trailingAnchor.constraint(equalTo: topContainer.trailingAnchor).isActive = true
model.heightAnchor.constraint(equalTo: topContainer.heightAnchor).isActive = true
}
func bottomContainerLayout(){
bottomContainer.topAnchor.constraint(equalTo: topContainer.bottomAnchor).isActive = true
bottomContainer.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
bottomContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
bottomContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
func topContainerLayout(){
topContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 2/3 , constant: -20).isActive = true
topContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
#objc func someButtonAction(){
print("Button is tapped!")
let opt = OTPViewController()
present(opt, animated: true, completion: nil)
}
}
`
If you want to customize app orientation do this:
Go to General setting of project
in Deployment , change device orientation .
Probably you didn't make auto layout.
Please check this out -> Auto Layout Guide
And this -> Programmatically Creating Constraints
Edit:
Use that translatesAutoresizingMaskIntoConstraints = false after addSubView.For example
func topContainerLayout(){
topContainer.translatesAutoresizingMaskIntoConstraints = false
topContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 2/3 , constant: -20).isActive = true
topContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}

Setting background color for customView has no effect

I have a customView which I display by after a button-tap. My problem is that setting .backgroundColor has no effect and it is just a clear background.
CustomView:
let wishWishView: WishStackView = {
let v = WishStackView()
v.backgroundColor = .cyan
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
AddViewButton:
#objc func addWishButtonTapped(){
self.view.addSubview(self.wishWishView)
wishWishView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
wishWishView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
}
WishWishView is just a simple UIView with a StackView inside of it.
My guess would be that it is a UIStackView, which does not support having a background color.
You can read more about why this happens, and possible workarounds here: https://stackoverflow.com/a/34868367/3992237
Your code is Totally wrong, first you set your view like this:
let wishWishView: UIView = {
let v = UIView()
v.backgroundColor = .cyan
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
now set your stackView, in my example I put in 2 label in vertical axis:
let stackView: UIStackView = {
let label1 = UILabel()
label1.text = "Label 1"
label1.textColor = .red
label1.font = UIFont.systemFont(ofSize: 16)
let label2 = UILabel()
label2.text = "Label 2"
label2.textColor = .black
label2.font = UIFont.systemFont(ofSize: 16)
let sV = UIStackView(arrangedSubviews: [label1, label2])
sV.axis = .vertical
sV.distribution = .fillEqually
sV.translatesAutoresizingMaskIntoConstraints = false
return sV
}()
after that set your button:
let buttonAddView: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add View", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .red
button.addTarget(self, action: #selector(addWishButtonTapped), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
in viewDidLoad add button and set constraints
view.addSubview(buttonAddView)
buttonAddView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
buttonAddView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
buttonAddView.heightAnchor.constraint(equalToConstant: 50).isActive = true
buttonAddView.widthAnchor.constraint(equalToConstant: 120).isActive = true
now write the function that add the view with stack view inside when the button is tapped with right constraints, in your function you call only the position of the view but not the size of it. Write your function like this:
#objc func addWishButtonTapped(){
view.addSubview(self.wishWishView)
wishWishView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
wishWishView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
wishWishView.heightAnchor.constraint(equalToConstant: 100).isActive = true
wishWishView.widthAnchor.constraint(equalToConstant: 200).isActive = true
wishWishView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: wishWishView.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: wishWishView.bottomAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: wishWishView.leadingAnchor, constant: 10).isActive = true
stackView.trailingAnchor.constraint(equalTo: wishWishView.trailingAnchor, constant: -10).isActive = true
}
Now simply change background of the wishWishView to automatically set stackView background, and that's it...

Resources