Close Button Not Working in Swift Playground - ios

I am working on a Swift playground for WWDC. I need to have a UI Button that closes the web view and toolbars (the button is called closeButton and the function is at the bottom for the button), and returns to the previously screen when clicked (the first view). The code below is the Swift I have typed so far:
import UIKit
import WebKit
import PlaygroundSupport
import Foundation
PlaygroundPage.current.needsIndefiniteExecution = true
class MyViewController : UIViewController {
let cardView = UIView()
let accountButton = UIButton()
let coverImageView = UIImageView()
let swiftLogoView = UIImageView()
let openLabel = UILabel()
let titleLabel = UILabel()
let label = UILabel()
let captionLabel = UILabel()
let descriptionLabel = UILabel()
let backgroundImageView = UIImageView()
let closeButton = UIButton()
let openButton = UIButton()
let doneButton = UIButton()
let url1 = URL(string: "https://youtube.com/embed/uuxXHAKA1WY")!
let alertController = UIAlertController(title: "Who Made This Playground:", message: "Mark Bruckert made this playground for WWDC 2018. He is a Web and Graphic Designer learning Swift and IOS Design. He would love to attend this year, to learn the new frameworks revealed at the event and have IOS Engineers review his designs and code.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close", style: .default, handler: nil)
let menuBar = UIView()
let doneLabel = UILabel()
let webView = UIWebView()
let cardView2 = UIView()
let coverImageView2 = UIImageView()
let titleLabel2 = UILabel()
let captionLabel2 = UILabel()
let descriptionLabel2 = UILabel()
let backgroundImageView2 = UIImageView()
let url2 = URL(string: "https://youtu.be/uuxXHAKA1WY")!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
label.frame = CGRect(x: 20, y: 30, width: 272, height: 38)
label.text = "Dev Tutorials:"
label.textColor = .black
label.font = UIFont.systemFont(ofSize: 32, weight: .bold)
openLabel.frame = CGRect(x: 140, y: 215, width: 272, height: 38)
openLabel.text = "Play Video"
openLabel.textColor = .black
openLabel.font = UIFont.systemFont(ofSize: 32, weight: .semibold)
self.openLabel.alpha = 0
openLabel.layer.zPosition = 5
doneLabel.frame = CGRect(x: 25, y: 5, width: 272, height: 38)
doneLabel.text = "Done"
doneLabel.textColor = .white
doneLabel.font = UIFont.systemFont(ofSize: 32, weight: .light)
openLabel.layer.zPosition = 7
doneButton.addTarget(self, action: #selector(doneButtonTapped), for: .touchUpInside
)
cardView.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
cardView.layer.cornerRadius = 14
cardView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
cardView.layer.shadowOpacity = 0.25
cardView.layer.shadowOffset = CGSize(width: 0, height: 10)
cardView.layer.shadowRadius = 10
cardView2.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
cardView2.layer.cornerRadius = 14
cardView2.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
cardView2.layer.shadowOpacity = 0.25
cardView2.layer.shadowOffset = CGSize(width: 0, height: 10)
cardView2.layer.shadowRadius = 10
titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
titleLabel.text = "Portals with ARKit"
titleLabel.textColor = .white
titleLabel.font = UIFont.systemFont(ofSize: 32, weight: .semibold)
captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
captionLabel.text = "by Jared Davidson"
captionLabel.textColor = .white
captionLabel.numberOfLines = 0
descriptionLabel.frame = CGRect(x: 20, y: 400, width: 335, height: 132)
descriptionLabel.text = "In this tutorial, you will learn how to use ARKit by Apple to transport yourself through a portal."
descriptionLabel.textColor = .black
descriptionLabel.numberOfLines = 10
descriptionLabel.alpha = 0
coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
coverImageView.contentMode = .scaleAspectFill
coverImageView.image = #imageLiteral(resourceName: "Cover.jpg")
coverImageView.clipsToBounds = true
swiftLogoView.frame = CGRect(x: 8, y: 8, width: 35, height: 35)
swiftLogoView.contentMode = .scaleAspectFill
swiftLogoView.image = #imageLiteral(resourceName: "Swift_logo.png")
swiftLogoView.clipsToBounds = true
coverImageView.layer.cornerRadius = 14
accountButton.frame = CGRect(x: 360, y: 20, width: 55, height: 55)
accountButton.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
accountButton.layer.cornerRadius = 30
accountButton.addTarget(self, action: #selector(accountButtonTapped), for: .touchUpInside)
closeButton.frame = CGRect(x: 360, y: 20, width: 28, height: 28)
closeButton.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5)
closeButton.layer.cornerRadius = 14
closeButton.setImage(#imageLiteral(resourceName: "Action-Close#2x.png"), for: .normal)
closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
closeButton.alpha = 0
openButton.frame = CGRect(x: 100, y: 200, width: 220, height: 75)
openButton.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
openButton.layer.cornerRadius = 14
openButton.addTarget(self, action: #selector(openButtonTapped), for: .touchUpInside)
openButton.alpha = 0
doneButton.frame = CGRect(x: 10, y: 5, width: 130, height: 50)
doneButton.layer.borderWidth = 3.0
doneButton.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
doneButton.layer.cornerRadius = 14
doneButton.addTarget(self, action: #selector(doneButtonTapped), for: .touchUpInside)
cardView.addSubview(coverImageView)
cardView.addSubview(openLabel)
doneButton.addSubview(doneLabel)
cardView.addSubview(closeButton)
cardView.addSubview(openButton)
menuBar.addSubview(doneButton)
cardView.addSubview(descriptionLabel)
view.addSubview(cardView)
view.addSubview(label)
view.addSubview(accountButton)
let tap = UITapGestureRecognizer(target: self, action: #selector(cardViewTapped))
cardView.addGestureRecognizer(tap)
cardView.isUserInteractionEnabled = true
doneButton.isUserInteractionEnabled = true
self.view = view
}
#objc func cardViewTapped() {
let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) {
self.cardView.frame = CGRect(x: 0, y: 0, width: 450, height: 667)
self.label.layer.isHidden = true
self.cardView.layer.cornerRadius = 0
self.titleLabel.frame = CGRect(x: 20, y: 20, width: 374, height: 38)
self.captionLabel.frame = CGRect(x: 20, y: 370, width: 272, height: 40)
self.descriptionLabel.alpha = 1
self.coverImageView.frame = CGRect(x: 0, y: 0, width: 450, height: 420)
self.coverImageView.layer.cornerRadius = 0
self.closeButton.alpha = 1
self.openButton.alpha = 0.7
self.accountButton.alpha = 0
self.openLabel.alpha = 1
}
animator.startAnimation()
}
#objc func closeButtonTapped() {
let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) {
self.cardView.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
self.cardView.layer.cornerRadius = 14
self.titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
self.captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
self.descriptionLabel.alpha = 0
self.coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
self.coverImageView.layer.cornerRadius = 14
self.closeButton.alpha = 0
self.label.layer.isHidden = false
self.openButton.alpha = 0
self.openLabel.alpha = 0
self.accountButton.alpha = 1
}
animator.startAnimation()
}
#objc func openButtonTapped() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
view.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = view
menuBar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(menuBar)
menuBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
menuBar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
let toolbar = UIView()
toolbar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbar)
toolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
toolbar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
toolbar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
toolbar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
toolbar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: menuBar.bottomAnchor, constant: 8.0).isActive = true
webView.bottomAnchor.constraint(equalTo: toolbar.topAnchor, constant: -8.0).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.loadRequest(URLRequest(url: URL(string:"https://youtube.com/embed/uuxXHAKA1WY")!))
}
#objc func accountButtonTapped() {
present(alertController, animated: true, completion: nil)
alertController.addAction(defaultAction)
}
#objc func doneButtonTapped() {
self.webView.alpha = 0
self.view.alpha = 0
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Thanks in advance for your help,
Mark B.

In method openButtonTapped, Local variable view created problem to your code. Also, you called liveView again. I have modified your code, it is working assumed that last screen will go to previous.
First, declare the toolBar as member variable like menuBar.And you the following code. Tap done button will go to previous.
#objc func openButtonTapped() {
view.backgroundColor = UIColor.cyan
menuBar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(menuBar)
menuBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
menuBar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
toolbar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbar)
toolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
toolbar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
toolbar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
toolbar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
toolbar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: menuBar.bottomAnchor, constant: 8.0).isActive = true
webView.bottomAnchor.constraint(equalTo: toolbar.topAnchor, constant: -8.0).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.loadRequest(URLRequest(url: URL(string:"https://youtube.com/embed/uuxXHAKA1WY")!))
}
#objc func accountButtonTapped() {
present(alertController, animated: true, completion: nil)
alertController.addAction(defaultAction)
}
#objc func doneButtonTapped() {
print("test")
self.webView.stopLoading()
self.webView.alpha = 0
toolbar.removeFromSuperview()
menuBar.removeFromSuperview()
}

Related

UITextField - bottom line programmatically

I am trying to add a bottom line to UITextField ...
what am I doing wrong?
let emialTextField: UITextField = {
let textField = UITextField()
textField.layer.cornerRadius = 5
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
Try below code.
override func viewDidLoad() {
super.viewDidLoad()
let emialTextField: UITextField = {
let textField = UITextField(frame: CGRect(x: 100, y: 90, width: 100, height: 30))
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height + 1, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 3/255, green: 4/255, blue: 5/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
view.addSubview(emialTextField)
emialTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100).isActive = true
emialTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 90).isActive = true
emialTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
emialTextField.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
It seems textField.frame.width is zero when executing below line of your code:
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
So you should add bottomline layer to your text field after size set for emialTextField via constraint or frame size.

NavigationItem.titleView is not getting the right size

I'm trying to set a custom view on a large navigationBar, but it insists on staying at 44px.
func setLargeNavBarForSection(_ section: String, subTitle: String, studyLogoName: String) -> Void {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.barTintColor = .cyan
navigationBar.shadowImage = UIImage()
let customView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBar.frame.width, height: 96.0))
let sectionNameFrame = CGRect(x: 80, y : 23, width : 150, height : 41)
let subTitleFrame = CGRect(x: 80, y: 53, width: 200, height: 14)
let studyImageView = UIImageView(frame: CGRect(x: 20, y: 25, width: 45, height: 45))
studyImageView.image = UIImage(named: studyLogoName)?.imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
studyImageView.contentMode = .scaleAspectFit
studyImageView.backgroundColor = .white
studyImageView.layer.cornerRadius = 22
studyImageView.addShadow(radius: 3, opacity: 0.1, x: 1, y: 2)
let myStudiesButton = UIButton(frame: CGRect(x: navigationBar.frame.width-96, y: 36, width: 76, height: 26))
myStudiesButton.setImage(UIImage(named: "switchStudies"), for: .normal)
myStudiesButton.addTarget(self, action: Selector(("switchStudies")), for: .touchUpInside)
myStudiesButton.showsTouchWhenHighlighted = true
let sectionNameLabel = UILabel(frame: sectionNameFrame)
sectionNameLabel.text = section
sectionNameLabel.textAlignment = .left
sectionNameLabel.font = UIFont(name: "Montserrat-Bold", size: 18)
sectionNameLabel.textColor = .black
let subTitleLabel = UILabel(frame: subTitleFrame)
subTitleLabel.text = subTitle
subTitleLabel.textAlignment = .left
subTitleLabel.font = UIFont(name: "Montserrat-Medium", size: 11)
subTitleLabel.textColor = .blueGrey
// customView.addSubview(studyImageView)
// customView.addSubview(myStudiesButton)
// customView.addSubview(sectionNameLabel)
// customView.addSubview(subTitleLabel)
navigationItem.largeTitleDisplayMode = .always
customView.backgroundColor = .red
navigationItem.titleView = customView
}
}
navigationController?.navigationBar.prefersLargeTitles = true get called in the viewDidLoad on the ViewController.

Activity Indicator not appears center on custom UIAlertView when search view controller push ups VC

I want to show the activity indicator on UIAlertViewController which contain the table view, So its working perfectly, but second case is when search controller appears on the Parent of UIAlertViewController then activity indicator not appears in center on UIAlertViewController.
Attaching image before attaching searchVC on Parent
And when searchVC added on Parent then
Following is code of activity indicator
var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
func startActivityIndicator() {
self.view.isUserInteractionEnabled = false
let loadingView: UIView = UIView()
loadingView.frame = CGRect(x: 0, y: 0, width: 80.0, height: 80.0)
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(red: 44/255, green: 44/255, blue: 44/255, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
actInd.frame = CGRect(x: 0, y: 0, width: 40.0, height: 40.0)
actInd.style = .whiteLarge
actInd.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2)
loadingView.addSubview(actInd)
self.view.addSubview(loadingView)
actInd.startAnimating()
}
func stopActivityIndicator() {
self.view.isUserInteractionEnabled = true
actInd.stopAnimating()
let view = actInd.superview
view?.removeFromSuperview()
}
above code I have added in extension on UIViewController
Finally solved it by setting constraints by following way
func startActivityIndicator() {
self.view.isUserInteractionEnabled = false
let loadingView: UIView = UIView()
loadingView.translatesAutoresizingMaskIntoConstraints = false
actInd.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(loadingView)
loadingView.addSubview(actInd)
loadingView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
loadingView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
loadingView.widthAnchor.constraint(equalToConstant: 80.0).isActive = true
loadingView.heightAnchor.constraint(equalToConstant: 80.0).isActive = true
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(red: 44/255, green: 44/255, blue: 44/255, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
actInd.leadingAnchor.constraint(equalTo: loadingView.leadingAnchor).isActive = true
actInd.trailingAnchor.constraint(equalTo: loadingView.trailingAnchor).isActive = true
actInd.topAnchor.constraint(equalTo: loadingView.topAnchor).isActive = true
actInd.bottomAnchor.constraint(equalTo: loadingView.bottomAnchor).isActive = true
actInd.style = .whiteLarge
actInd.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2)
actInd.startAnimating()
}

Custom NavigationBar in iOS Swift

I'm creating custom navigationbar in swift for every screen. I'm calling this function at viewWillAppear while first time loading it is coming properly if First Time Loading Screen. I move to another screen it is changing. Next View screen navigation title view is shifted. if I came back to previous screen then also it was shiftedenter image description here. I'm unable to find the bug where code was wrong.left and right navigation items are placing fine . But navigationtitle view only getting shifting.
extension UIViewController {
func reightMenuItems(_ isMenuBtn: Bool, _ navigationItem:UINavigationItem) {
if isMenuBtn {
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "MenuIcon"))
menuImage.contentMode = .scaleAspectFit
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 32)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
followButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)
followButton.addTarget(self, action: #selector(self.btnClickMenu), for: .touchUpInside)
leftView.addSubview(followButton)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}else{
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "arrow_white-left"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 12.5, y: 10.5, width: 15, height: 25)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
leftView.addSubview(followButton)
followButton.addTarget(self, action: #selector(self.btnClickBack), for: .touchUpInside)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}
}
fileprivate func leftMenuItems(_ isCart: Bool, _ navigationItem: UINavigationItem) {
//lecftButtons
if isCart {
let reightView = UIView()
reightView.backgroundColor = .clear
reightView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "Cart"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 34)
reightView.addSubview(menuImage)
let cartBtn = UIButton(type: .system)
cartBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
reightView.addSubview(cartBtn)
cartBtn.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: reightView)]
}else{
let composeButton = BtnFontBold20()
composeButton.setTitle("Done", for: .normal)
composeButton.titleLabel?.textColor = .white
composeButton.frame = CGRect(x: 0, y: 00, width: 34, height: 44)
composeButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)
composeButton.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton)]
}
}
func setupNavItems(_ navigationItem: UINavigationItem , day:String = "",address:String = "",type:Int,isMenuBtn:Bool,isCart:Bool,title:String = "") {
//NavigationItem
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
//Shadow
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
self.navigationController?.navigationBar.layer.shadowRadius = 2.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.5
//reight Menu Items
reightMenuItems(isMenuBtn, navigationItem)
leftMenuItems(isCart, navigationItem)
let height = navigationController?.navigationBar.frame.height ?? 0
let leftButtonWidth: CGFloat = 90 // left padding
let width2 = AppConstants.screenWidth - leftButtonWidth
print(width2)
let Titleview = UIView(frame: CGRect(x: 0, y: 0, width: width2, height: height))
Titleview.backgroundColor = .clear
navigationItem.titleView = Titleview
if type == 1 {
let dayLbl = FontBold18(frame: CGRect(x: 0, y: 0, width: min(width2/2,40), height: height))
dayLbl.text = day
dayLbl.textColor = .white
dayLbl.textAlignment = .left
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
let DirectImag = UIImageView()
DirectImag.contentMode = .scaleAspectFit
DirectImag.image = #imageLiteral(resourceName: "RightDirection")
Titleview.addSubview(DirectImag)
DirectImag.translatesAutoresizingMaskIntoConstraints = false
DirectImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.leadingAnchor.constraint(equalTo: dayLbl.trailingAnchor, constant:5).isActive = true
DirectImag.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
let dayBtn = UIButton(type: .system)
dayBtn.addTarget(self, action: #selector(self.btnClickASAPType), for: .touchUpInside)
Titleview.addSubview(dayBtn)
dayBtn.translatesAutoresizingMaskIntoConstraints = false
dayBtn.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
dayBtn.centerXAnchor.constraint(equalTo: dayLbl.centerXAnchor, constant: 0).isActive = true
dayBtn.heightAnchor.constraint(equalTo: dayLbl.heightAnchor).isActive = true
dayBtn.widthAnchor.constraint(equalTo: dayLbl.widthAnchor , constant:25).isActive = true
let addLbl = FontBold18()
addLbl.textColor = .white
addLbl.textAlignment = .left
addLbl.numberOfLines = 1
Titleview.addSubview(addLbl)
addLbl.text = address
addLbl.translatesAutoresizingMaskIntoConstraints = false
addLbl.leadingAnchor.constraint(equalTo: DirectImag.trailingAnchor,constant:5).isActive = true
addLbl.heightAnchor.constraint(equalToConstant: height).isActive = true
addLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
addLbl.widthAnchor.constraint(lessThanOrEqualToConstant: (width2 - dayLbl.frame.width) * 0.9).isActive = true
let downImag = UIImageView()
downImag.contentMode = .scaleAspectFit
downImag.image = #imageLiteral(resourceName: "down-arrow-white")
Titleview.addSubview(downImag)
downImag.translatesAutoresizingMaskIntoConstraints = false
downImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
downImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
downImag.leadingAnchor.constraint(equalTo: addLbl.trailingAnchor, constant:5).isActive = true
downImag.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
let addressBtn = UIButton(type: .system)
addressBtn.backgroundColor = .clear
addressBtn.addTarget(self, action: #selector(self.btnClickAddress), for: .touchUpInside)
Titleview.addSubview(addressBtn)
addressBtn.translatesAutoresizingMaskIntoConstraints = false
addressBtn.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
addressBtn.leadingAnchor.constraint(equalTo: addLbl.leadingAnchor, constant: 0).isActive = true
addressBtn.heightAnchor.constraint(equalTo: addLbl.heightAnchor).isActive = true
addressBtn.widthAnchor.constraint(equalTo: addLbl.widthAnchor).isActive = true
}
else if type == 2{
let dayLbl = FontBold18()
dayLbl.text = title
dayLbl.textColor = .white
dayLbl.textAlignment = .center
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
dayLbl.translatesAutoresizingMaskIntoConstraints = false
dayLbl.centerXAnchor.constraint(equalTo: Titleview.centerXAnchor).isActive = true
dayLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
}
}
#objc func btnClickASAPType() {
print("DayExt")
// self.overriderFunc()
}
#objc func btnClickAddress() {
print("AddressExt")
}
#objc func btnClickCart() {
print("CartExt")
}
#objc func btnClickMenu() {
print("MenuExt")
}
#objc func btnClickBack() {
print("backExt")
self.navigationController?.popViewController(animated: true)
}
}
calling setupNavigation Method in viewDidLoad
self.setupNavItems( self.navigationItem, day:"Today", address:"Jayanagar 4th Block", type: 1,isMenuBtn: false,isCart: true)

Center a UILabel of a UIView

I am currently trying to make a simple pop up, here I have a button, once it gets tapped.
It should show a simple view above it, I achieved the blue background, but I am not being able to add a label to it, and center the label to the blue popup
let width = (sender as! UIButton).frame.width
let y = (sender as! UIButton).frame.origin.y
let x = (sender as! UIButton).frame.origin.x
myView = UIView(frame: CGRect(x: x, y: y - 30, width:width, height: 30))
myView.backgroundColor = UIColor.blue
let label = UILabel()
label.text = (sender as! UIButton).titleLabel?.text
label.font = UIFont(name:"avenirNext-Meduim",size:20)
label.center = self.myView.center
myview.addSubview(label)
self.view.addSubview(myView)
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations: {
self.myView.alpha = 0.0
}) { (finished: Bool) in
self.myView.isHidden = true
}
Try to set your label frame:
let label = UILabel(frame: CGRect(origin: .zero, size: yourLabelSize))
Instead of
label.center = self.myView.center
try
label.centerXAnchor.constraint(equalTo: myView.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
after
myview.addSubview(label)
self.view.addSubview(myView)
My solution
let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: myView.center.x, y: myView.center.y, width: 51, height: 30)
codedLabel.textAlignment = .center
codedLabel.text = "q"
codedLabel.numberOfLines = 1
codedLabel.textColor=UIColor.red
codedLabel.font=UIFont.systemFont(ofSize: 22)
codedLabel.backgroundColor=UIColor.lightGray
self.myView.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.centerYAnchor.constraint(equalTo: codedLabel.superview!.centerYAnchor).isActive = true
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
let label = UILabel()
label.frame = CGRect.init(x: headerView.center.x-60, y:headerView.center.y-15, width: 120, height: 30)
label.textAlignment = .center
label.textColor = #colorLiteral(red: 0.1290470958, green: 0.2743584514, blue: 0.6418049335, alpha: 1)
label.font = UIFont(name: "Roboto-Bold", size: 15)
label.text = "11-12-2020"
headerView.addSubview(label)
headerView.backgroundColor = #colorLiteral(red: 0.9967060685, green: 0.9998621345, blue: 0.9997505546, alpha: 1)

Resources