Skip to content

Instantly share code, notes, and snippets.

@sinbadbd
Last active May 10, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinbadbd/1e6428971fa8326806bd35813e71c819 to your computer and use it in GitHub Desktop.
Save sinbadbd/1e6428971fa8326806bd35813e71c819 to your computer and use it in GitHub Desktop.
UIView height adjust any content
//
// ViewController.swift
// LoveiOS
//
// Created by Imran on 9/5/20.
// Copyright © 2020 portonics. All rights reserved.
//
import UIKit
extension UIView {
// OUTPUT 1
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
// OUTPUT 2
func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOpacity = opacity
layer.shadowOffset = offSet
layer.shadowRadius = radius
layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
class ViewController: UIViewController {
let contentView = UIView()
let scrollView = UIScrollView()
let mainContentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// view.backgroundColor = .red
setupScrollView()
}
func setupScrollView(){
scrollView.translatesAutoresizingMaskIntoConstraints = false
mainContentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.addSubview(mainContentView)
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
mainContentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
mainContentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
mainContentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
mainContentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
mainContentView.addSubview(contentView)
contentView.backgroundColor = UIColor.init(red: 247/255, green: 242/255, blue: 237/255, alpha: 1)
contentView.layer.cornerRadius = 4
contentView.layer.shadowColor = UIColor.darkGray.cgColor
contentView.layer.shadowOpacity = 0.5
contentView.layer.shadowOffset = .zero
contentView.layer.shadowRadius = 2
// contentView.dropShadow(color: .red, offSet: CGSize(width: 1, height: 1))
// contentView.dropShadow(color: .red, opacity: 1, offSet: CGSize(width: -1, height: 1), radius: 3, scale: true)
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.leftAnchor.constraint(equalTo: mainContentView.leftAnchor, constant: 10).isActive = true
contentView.topAnchor.constraint(equalTo: mainContentView.topAnchor, constant: 10).isActive = true
contentView.rightAnchor.constraint(equalTo: mainContentView.rightAnchor, constant: -10).isActive = true
contentView.bottomAnchor.constraint(equalTo: mainContentView.bottomAnchor, constant: -50).isActive = true
contentView.setContentHuggingPriority(.init(rawValue: 250), for: .vertical)
contentView.addSubview(textLabel)
textLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
textLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
textLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10).isActive = true
// textLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
contentView.addSubview(buttonTap)
buttonTap.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: 10).isActive = true
// buttonTap.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
buttonTap.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10).isActive = true
buttonTap.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
buttonTap.heightAnchor.constraint(equalToConstant: 30).isActive = true
buttonTap.widthAnchor.constraint(equalToConstant: 80).isActive = true
}
let textLabel : UILabel = {
let text = UILabel()
text.text = "Here you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative waterHere you increment or decrement the counter depending on the button’s isAddButton. Though you could set the counter to fewer than zero, it probably won’t work with Flo. Nobody can drink negative water00"
text.numberOfLines = 0
text.translatesAutoresizingMaskIntoConstraints = false
return text
}()
let buttonTap : UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("View More", for: .normal)
button.setTitleColor(.link, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
button.backgroundColor = .lightText
button.layer.cornerRadius = 4
return button
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment