Can't display view in playground - swift

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

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)

how to resize a view in live playgrounds ?? Xcode

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

How to show an image in Xcode Playground with NSImage

I can actually preview my image when I click on the eye in the side bar. However, the last line in the code doesn't show anything.
//: A Cocoa based Playground to present user interface
import AppKit
import PlaygroundSupport
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
let imgView = NSImageView(frame:NSRect(x: 0, y:0 , width: 300, height: 400))
imgView.image = NSImage(byReferencingFile: "/Users/me/Code/MyPlayground.playground/Resources/image.png")
PlaygroundPage.current.liveView = imgView
Open the Project Navigator to left side
Select Resources folder
Right click then "Add files to 'Resources' "
You can use like that -> imgView.image = UIImage(named: "image_name")
You can see in this image

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)

NSVisualEffectBlendingMode OS X Swift

I want to be able to make the background of my application blurry so you can almost see through the window like this
rather than the standard window
Thank you!
I Found out the answer I was looking for and thought this might be useful for other people look for something similar. Heres my code :
import Cocoa
class window: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
let view = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: 1500, height: 800))
view.blendingMode = NSVisualEffectBlendingMode.BehindWindow
view.material = NSVisualEffectMaterial.Dark
view.state = NSVisualEffectState.Active
self.window!.contentView!.addSubview(view)
}
}
I made a NSWindowController class and added the code above to it. Then connected the class to the window controller in my main.storyboard.