keyboardwasshown animated swift - ios

there is UITextView at the bottom of design .When I try to write something keyboard stays down because of that I increase it as keyboard height but there is a problem. Keyboard comes from before TextView and it doesn't look nice .However on the Whatsapp and swarm applications it doesn't happen like this keyboard increasing with the TextView at the same time . How can I make them timing . You can see the my codes on the below
Sincerely
var frameMessageView = UIView()
var mesajtext = UITextView()
var resultScrollView = UIScrollView()
var lblcizgi = UILabel();
var topImg = UIImageView();
let place = ""
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
let backButton = UIBarButtonItem(title: "Geri", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack:")
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 15.0)!], forState: UIControlState.Normal)
self.tabBarController?.tabBar.hidden = true
webView.delegate = self
webView.scrollView.bounces = false
resultScrollView.frame = CGRectMake(0, 67, theWidth, theHeight-112)
resultScrollView.backgroundColor = UIColor.blueColor()
topImg.frame = CGRectMake(0, 64, theWidth, 3)
topImg.image = UIImage(named:"top-bottom")
webView.frame = CGRectMake(0, 0, self.resultScrollView.frame.size.width, UIScreen.mainScreen().bounds.height-102)
view.backgroundColor = UIColor.blackColor()
frameMessageView.frame = CGRectMake(0, theHeight - 45, theWidth, 45)
frameMessageView.backgroundColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
mesajtext.font = UIFont(name: "Helvetica Neue", size: 15.0)
mesajtext.frame = CGRect(x: 10, y: 10, width: self.view.frame.size.width - 75, height: 27)
mesajtext.backgroundColor = UIColor.whiteColor()
mesajtext.layer.cornerRadius = 5.0
mesajtext.layer.masksToBounds = true
mesajtext.layer.borderColor = UIColor( red: 203/255, green: 203/255, blue:203/255, alpha: 1.0 ).CGColor
mesajtext.layer.borderWidth = 1.0
lblcizgi.frame = CGRectMake(0, 0, frameMessageView.frame.width, 1)
lblcizgi.backgroundColor = UIColor(red: 203/255, green: 203/255, blue: 203/255, alpha: 0.5)
let button = UIButton()
button.frame = CGRectMake(self.view.frame.maxX-65, 13, 60, 20)
//button.backgroundColor = UIColor(red: 251.0/255.0, green: 188.0/255.0, blue: 5.0/255.0, alpha: 1.0)
button.setTitle("Gönder", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 27/255, green: 119/255, blue: 218/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "gonderbutonclick:", forControlEvents: UIControlEvents.TouchUpInside)
button.titleLabel!.font = UIFont(name:"Helvetica Neue", size: 14.0)
applyPlaceholderStyle(mesajtext , placeholderText: place)
mesajtext.delegate = self
frameMessageView.addSubview(lblcizgi)
frameMessageView.addSubview(mesajtext)
frameMessageView.addSubview(button)
view.addSubview(frameMessageView)
view.addSubview(resultScrollView)
view.addSubview(topImg)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
let dict:NSDictionary = notification.userInfo!
let s:NSValue = dict.valueForKeyPath(UIKeyboardFrameEndUserInfoKey) as! NSValue
let rect:CGRect = s.CGRectValue()
UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
self.resultScrollView.frame = CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.height-rect.height-112)
self.frameMessageView.frame.origin.y = self.resultScrollView.frame.maxY
}) { (Bool) -> Void in
}
}

Related

How to set action in label?

Here is my code:
added gesture in viewDidLoad
let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
toastLabel.addGestureRecognizer(tap)
toastLabel.isUserInteractionEnabled = true
func showLongToast( message: String) {
toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))
toastLabel.numberOfLines = 0
toastLabel.textAlignment = .center
toastLabel.contentMode = .center
toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
let string = NSMutableAttributedString(string: trimmedString)
string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
toastLabel.attributedText = string
toastLabel.layer.cornerRadius = 25
toastLabel.clipsToBounds = true
controller.view.addSubview(toastLabel)
controller.view.bringSubviewToFront(toastLabel)
}
I have call the function from viewController :
showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)
But toast message could not set action anymore? Have any idea
please post in comment. Thanks.
This is how your viewDidLoad() and tapLabel(tap:) should look like,
override func viewDidLoad() {
super.viewDidLoad()
self.showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
toastLabel.addGestureRecognizer(tap)
toastLabel.isUserInteractionEnabled = true
}
#objc func tapLabel(tap: UITapGestureRecognizer) {
print("tapped..!!!")
}
And the signature of showLongToast won't compile with your code. It should be like,
func showLongToast( message: String, controller: UIViewController) {
//your code here...
}
You are adding the tap gesture recognizer to toastLabel in viewDidLoad(), but then you are creating a new UILabel inside showLongToast()
Change:
toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))
in showLongToast() to:
toastLabel.frame = CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
toastLabel.addGestureRecognizer(tap)
toastLabel.isUserInteractionEnabled = true
}
func showLongToast( message: String) {
toastLabel.frame = CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)
toastLabel.numberOfLines = 0
toastLabel.textAlignment = .center
toastLabel.contentMode = .center
toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
let string = NSMutableAttributedString(string: trimmedString)
string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
toastLabel.attributedText = string
toastLabel.layer.cornerRadius = 25
toastLabel.clipsToBounds = true
controller.view.addSubview(toastLabel)
controller.view.bringSubviewToFront(toastLabel)
}

Setting background of game view controller

I would like to change the background of my game view controller,
However the classic self.view.backgroundColor = UIColor.red for example isn't working and I am not sure why. Could someone help me out? maybe I am missing something simple here is some code
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 35/255.0, green: 35/255.0, blue: 33/255.0, alpha: 1.0)
codedLabel.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
codedLabel.textAlignment = .center
codedLabel.text = "Box and Weave"
codedLabel.numberOfLines=1
codedLabel.textColor=UIColor.white
codedLabel.font=UIFont.systemFont(ofSize: 25)
codedLabel.backgroundColor = UIColor(red: 35/255.0, green: 35/255.0, blue: 33/255.0, alpha: 1.0)
self.view.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
playButton.translatesAutoresizingMaskIntoConstraints = false
playButton.backgroundColor = UIColor.white
playButton.setTitle("Play!", for: .normal)
playButton.setTitleColor(UIColor.blue, for: .normal)
playButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 23)
playButton.titleLabel?.adjustsFontSizeToFitWidth = true
playButton.titleLabel?.minimumScaleFactor = 0.5
playButton.layer.cornerRadius = 25
self.view.addSubview(playButton)
// contraints for button
let buttonHeight = playButton.heightAnchor.constraint(equalToConstant: 50)
let buttonWidth = playButton.widthAnchor.constraint(equalToConstant: 125)
let xPlacement = playButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
let yPlacement = playButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
buttonConstraints = [buttonHeight, buttonWidth, xPlacement, yPlacement]
NSLayoutConstraint.activate(buttonConstraints)
playButton.addTarget(self, action: #selector(playButtonMethod), for: .touchUpInside)
}
This is how it looks when loaded

How to Change uitextfield leftView image tintColor on editing

I'm trying to achieve UITextField editing or not editing style like this:
But the trickiest part for me is How to change that left image tint color. I have achieved this so far:
My code:
UITextField
lazy var email: UITextField = {
let name = UITextField()
name.layer.cornerRadius = 17.5
name.layer.borderColor = UIColor(red: 0.55, green: 0.61, blue: 0.69, alpha: 0.5).cgColor
name.layer.borderWidth = 1.5
name.placeholder = "Email"
name.font = UIFont.systemFont(ofSize: 15)
name.textColor = UIColor(red: 0.55, green: 0.61, blue: 0.69, alpha: 1)
name.backgroundColor = .clear
name.leftViewMode = UITextFieldViewMode.always
name.delegate = self
name.translatesAutoresizingMaskIntoConstraints = false
return name
}()
leftImage func:
func addLeftImageTo(txtField: UITextField, andImage img: UIImage) {
let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 42.75, height: 40))
let centerX: CGFloat = (leftView.frame.midX) - (img.size.width / 2)
let centerY: CGFloat = (leftView.frame.midY) - (img.size.height / 2)
let leftImageView = UIImageView(frame: CGRect(x: centerX + 2 , y: centerY - 1, width: img.size.width, height: img.size.height))
leftImageView.contentMode = .scaleAspectFit
leftImageView.image = img
leftView.addSubview(leftImageView)
txtField.leftView = leftView
txtField.leftViewMode = .always
}
Adding leftImages:
let emailLeftImg = UIImage(named: "ic_txt_field_email")
addLeftImageTo(txtField: email, andImage: emailLeftImg!)
let passwordLeftImg = UIImage(named: "ic_txt_field_password")
addLeftImageTo(txtField: password, andImage: passwordLeftImg!)
editingBegains and Ending:
func textFieldDidBeginEditing(_ textField: UITextField) {
self.setTextBorder(textField: textField, color: UIColor.white, borderColor: UIColor.white, isSelected: true)
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.setTextBorder(textField: textField, color: UIColor.clear, borderColor: UIColor(red: 0.55, green: 0.61, blue: 0.69, alpha: 0.5), isSelected: false)
}
func setTextBorder(textField: UITextField, color: UIColor, borderColor: UIColor, isSelected: Bool) {
textField.backgroundColor = color
textField.layer.borderColor = borderColor.cgColor
textField.tintColor = UIColor(red: 1.0, green: 0.2, blue: 0.33, alpha: 1)
textField.layer.masksToBounds = false
if isSelected == true {
textField.layer.shadowRadius = 3.0
textField.layer.shadowColor = UIColor.black.cgColor
textField.layer.shadowOffset = CGSize(width: 0, height: 2)
textField.layer.shadowOpacity = 0.125
} else {
textField.layer.shadowRadius = 0
textField.layer.shadowColor = UIColor.clear.cgColor
textField.layer.shadowOffset = CGSize(width: 0, height: 0)
textField.layer.shadowOpacity = 0
}
}
I had tried adding this code to change Image color but it didn't work.
var picTintColor: Bool = false
In LeftImage func:
if picTintColor == true {
leftImageView.image = img.withRenderingMode(.alwaysTemplate)
leftImageView.tintColor = .blue
} else {
leftImageView.image = img
}
And in editingBegains and Ending func:
if isSelected == true {
picTintColor = true
} else {
picTintColor = false
}
I'm a complete noob in IOS programming so thanks for your patience and sorry for my bad english. Thanks!
According the code ,it actually can not pass isSelected signal to the leftImageView,maybe leftImageView get in laster is not the earlier ,or the signal not pass successly.
I suggest that a easy way to do, just in editingBegains and Ending: to do what you want change the imageview,like this:
func textFieldDidBeginEditing(_ textField: UITextField) {
let emailLeftImg = UIImage(named: "ic_txt_field_email")
addLeftImageTo(txtField: email, andImage: emailLeftImg!)
}
func addLeftImageTo(txtField: UITextField, andImage img: UIImage) {
let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 42.75, height: 40))
let centerX: CGFloat = (leftView.frame.midX) - (img.size.width / 2)
let centerY: CGFloat = (leftView.frame.midY) - (img.size.height / 2)
let leftImageView = UIImageView(frame: CGRect(x: centerX + 2 , y: centerY - 1, width: img.size.width, height: img.size.height))
leftImageView.contentMode = .scaleAspectFit
if picTintColor == true {
leftImageView.image = img.withRenderingMode(.alwaysTemplate)
leftImageView.tintColor = .blue
} else {
leftImageView.image = img
}
leftView.addSubview(leftImageView)
txtField.leftView = leftView
txtField.leftViewMode = .always
}
and in end delgate method ,just do that ,you can try this way to test.Hope to help you.

UIBarButtonItem size displays wrong

I have custom UINavigationBar(for JSQMessagesViewController) with some items. But I have some problems with leftBarButtonItem
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 28, width: screenSize.width, height: 78))
let navItem = UINavigationItem(title: chatName ?? "chat")
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 78, height: 78)
menuBtn.setImage(UIImage(named: "backButton"), for: .normal)
menuBtn.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
let menuBarItem = UIBarButtonItem(customView: menuBtn)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 78)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true
navItem.leftBarButtonItem = menuBarItem
navBar.titleTextAttributes = [.font : UIFont(name: "Acrom-Bold", size: 26)!, .foregroundColor : UIColor.white]
navBar.setItems([navItem], animated: false)
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.3)
navBar.isTranslucent = true
self.view.addSubview(navBar)
}
But my BackButton looks wrong.
What is the problem with the height of the button?
If a bar button item has a custom view, you must size the width of the custom view using internal autolayout constraints.
But you must not size the height larger than the runtime likes, as you are doing. Delete these lines:
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true

Close Button Not Working in Swift Playground

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()
}

Resources