how to resize a view in live playgrounds ?? Xcode - swift

I am planning to create a live playground for coming wwdc scholarships but I have no experience in creating live playgrounds because I have worked with single view apps so far
I actually don't know what size should be the view in my playground and I am not able to resize it can any one tell me how to do that , I have tried the below code but it gives me error and doesn't work please help me and any advice related to wwdc project is appreciated thank you
let main = viewController()
main.view.frame.size = CGSize(width: 300, height: 600)
this code gives error:= expression failed to parse, unknown error
Please tell me how to resize the view and also how should I do it ??

You should change entire frame or bounds properties to resize the view:
import UIKit
import PlaygroundSupport
class ViewController : UIViewController {
...
}
let viewController = ViewController()
viewController.view.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

When you use navbar it is important to size the NavigationController and not the ViewController.
Like this:
let vc = MyViewController()
let navBar = UINavigationController(rootViewController: vc)
navBar.view.frame = CGRect(x: 0, y: 0, width: 320, height: 568)
PlaygroundPage.current.liveView = navBar

Related

Swift , UIkit - Add collection view to toolBar, GalleryApp

i want to create a photo gallery view. I would like to look like a photo gallery in iPhone.
I have a problem with adding a collection view to toolbar. If I add only one image as UIImageView , everything works but when I add collection view, view not appear
This is my code - help
let toolbar = ToolBarCollectionView()
toolbar.view.translatesAutoresizingMaskIntoConstraints = false
toolbar.view.frame = navigationController?.toolbar.frame ?? CGRect(x: 0, y: 0, width: 25, height: 25)
let item = UIBarButtonItem(customView: toolbar.view)
setToolbarItems([item], animated: false)
navigationController?.toolbar.setShadowImage(UIImage(), forToolbarPosition: .bottom)

Can't display view in playground

Code:
import UIKit
import PlaygroundSupport
let simpleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 450))
simpleView.backgroundColor = UIColor.cyan
PlaygroundPage.current.liveView = simpleView
PlaygroundPage.current.needsIndefiniteExecution = true
I can see the view when I click the "eyes icon" but when I open the assistant view it displays nothing.
Can anybody tell my what I need to do?
Screenshot
I work it out.
Firstly I update the Xcode to 8.3.2, but not work, then I delete all the DerivedData cache and create a new project, copy the code, then it works

How to put AdMob Banner in SpriteKit?

I am creating an game in SpriteKit, and I don't know how to use AdMob banner in SpriteKit. I know how to use with Swift where you drag the view to your storyboard, and etc... But once again, Can anyone help me to use it in SpriteKit?
Getting this error when applying to place banner.
To get test ads on this device, call: request.testDevices = #[ *!********'" ];**
Everything is the exact same as with UIKit, except that you have to position it programmatically. Also make sure that this code goes in viewDidLoad in your GameViewController. Just add this line in:
BannerAd.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
All in all you banner ad code should look something like this:
var BannerAd = GADBannerView(adSize: kGADAdSizeBanner)
BannerAd.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
BannerAD.delegate = self
BannerAD.adUnitID = "ca-app-pub-##/##"
BannerAD.rootViewController = self
let request = GADRequest()
request.testDevices = [kGADSimulatorID, "*************************"]
BannerAD.loadRequest(request)
view.addSubview(BannerAd)

UIImageView and UIWebView in UIScrollView

I have added UIImageView with view frame height and width and below that UIWebView has to be loaded. I am not sure how to implement this. I tried to add both in UIScrollView. Image is perfectly displaying but WebView not loaded. Adding UIWebView to UIScrollView is not good practice as its below screen, I need to make it in scroll. I am not using Storyboard, all through programming.
Please suggest what should be the best approach to implement this. Wether to use UIWebView in UIScrollView or if added then why it's not loaded. It comes blank.
func viewDidLoad(){
var image = UIImage(frame: view.bounds)
var webView = UIWebView(frame: CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height))
var scrollView = UIScrollView()
scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height*2)
webView.delegate = self
webView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")))
scrollView.addSubview(image)
scrollView.addSubView(webView)
self.view.addSubview(scrollView)
}
func webViewDidFinishLoad(_ webView: UIWebView){
}
func webViewDidStartLoad(_ webView: UIWebView){
}
Thanks
Your code is fine.
Please check if you are getting this error:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
If you are getting this error, then add this in info.plist file:

How to display a string/int within a UIView in Swift?

this is a very simple question, but I can't find an answer anywhere. I am trying to display a high score within a CGRect, but cannot figure out exactly how to do it. I am currently trying to do this on a playground so that might have something to do with it. Thanks for the help.
You can make a UILabel in a Swift playground. Here's how it might look:
Here's the code from the playground:
import UIKit
let score = 200
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 20))
label.backgroundColor = UIColor.purpleColor()
label.textColor = UIColor.orangeColor()
label.text = "High Score: \(score)"
A CGRect is the frame or dimensions assigned to a UIView, not the UIView itself. I would suggest creating a UILabel and add that to your view. You can set the text on the UILabel to, in your case, the high score variable as a string. Im not sure if it is possible to do this in a playground, but I may be mistaken. A XCode project would work perfect for this, but again, im not sure about the playground.
To do this in a playground, it would look like this:
import UIKit
import XCPlayground
let view = UIView(frame: CGRectMake(0,0,500,500)) //create a view and give it a proper size
let label = UILabel(frame: CGRectMake(0,0,100,23)) //create a label with a size
label.text = "My Label" //give the label some text
view.backgroundColor = UIColor.lightGrayColor() //default view in Playground as black background
view.addSubview(label) //add the label to the view
XCPlaygroundPage.currentPage.liveView = view //tell playgound to display your view as the root view in the assistant editor
You need to open the assistant editor in the playground to see the result (the two circles icon in the upper right corner)