How to center my Playgrounds Live View - xcode

I am instantiating a playground live view using the following code:
import UIKit
import PlaygroundSupport
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
view.backgroundColor = .white
PlaygroundPage.current.liveView = view
The live view that appears is stuck to the top of the screen and I can't change it's location by changing the x and y values of the frame. How do I get the view to move/center on my screen?
Photo of how it comes out:

Related

Xcode 12 Change the size of the navigation bar self.navigationController

It's for a webview, I want the Navigation bar on top, But I want it very small. But no matter what value I put there is not working :(
For example, with this code I have the text in navigation bar red
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
but trying to change the height let's say 8, 80 or even 800 don't work :(
navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80)
is there a better way ?
Actually after iOS 11 Apple does not allow us to customize the height of navigation bar. you can do that with custom navigation bar
// Before iOS 11
class MyNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 80)
}
}
or creating a custom navigation controller
class MyNavigationController: UINavigationController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 80)
}
}
Community Bug Reports
Subviews of UINavigationBar don't obey view frame height
sizeThatFits not working
#iOS 11 Xcode 9 Navigation bar height not changes
BONUS
Check the gianfilippoci's answer to developer forum

How to make the keyboard same size as the view

In the following code, the UIKeyboard is bigger than the view.
Question: How to make the keyboard same size as the view?
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController, UITextFieldDelegate {
override func loadView() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = .white
let label = UITextField()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
label.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
view.addSubview(label)
self.view = view
}
#objc func myTargetFunction() {
print("It works!")
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
The only "solution" I discovered so far is to set the preferredContentSize of your UIViewController to the size of the UIScreen.main of the current Xcode Playground environment:
let vc = MyViewController()
vc.preferredContentSize = UIScreen.main.bounds.size
PlaygroundPage.current.liveView = vc

How to get a Canvas in a Swift Playground

I would like to get a canvas that I can use to draw on. The target would be an iOS based Swift playground. I searched the documentation and I could not found an object named Canvas, but if there is something similar to it then it would be good for me as well.
You can use something similar to this, note you don't have to use Sprite Kit classes.
import UIKit
import PlaygroundSupport
import SpriteKit
// Playground Container Setup
//
//
let containerWidth: CGFloat = 667.0
let containerHeight: CGFloat = 375.0
let containerCenter: CGPoint = CGPoint(x: (containerWidth/2), y: (containerHeight/2))
let containerView = SKView(frame: CGRect(x: 0.0, y: 0.0, width: containerWidth, height: containerHeight))
PlaygroundPage.current.liveView = containerView
let containterScene: SKScene = SKScene(size: CGSize(width: containerWidth, height: containerHeight))
containerView.presentScene(containterScene)
UPDATED
I've created a wrapper class to make this easier for everyone. Download the Playground here on github

Unable to load a scene in a playground with XCode 7 beta 3

I'm unable to load a scene in XCode 7 beta 3 playground... although it was working with Xcode 6.4.... is it bug or something as changed ?
ps : SCNScene(named: "ship.dae") returns nil.
In my Resources folder is ship.dae and texture.png (from the SceneKit template), and here is my code :
//: Playground - noun: a place where people can play
import SceneKit
import XCPlayground
var view = SCNView(frame: NSRect(x: 0, y: 0, width: 500, height: 500))
var scene = SCNScene(named: "ship.dae")
view.scene = scene
XCPShowView("view", view: view)
In Xcode 7
import XCPlayground
let view = // your view
XCPlaygroundPage.currentPage.liveView = view
In Xcode 8
import PlaygroundSupport
let view = // your view
PlaygroundPage.current.liveView = view

Does swift playground support UIKit?

I tried to create a UILabel in playground but failed. Does playground only support OS X development for now?
YES, it does!
File: New > File... > iOS > Source > Playground
import UIKit
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
lbl.text = "Hello StackOverflow!"
Then, save the file. (Or manually run it.) This will trigger the Playground to interpret UI related things. At this point, the word "UILabel" should appear on the right-hand side.
Now, to actually view what you've done, you've got to click on the "Quick View" eye on the right, or the white circle to open it in Assistant Editor:
Here's a screenshot of some basic things with UIImage working, etc.
(EDIT: minor text update to current CGRect syntax -- But, screenshots still show old syntax.)
Edited#2014-11-13: It seems the new xcode 6 had fixed this.
NO, It doesn't. But it's worth noting that you can import UIKit.
If you want to import UIKit you cound follow this:
View -> Utilities -> Show File Inspector (opt + cmd + 1)
On the right side of Xcode Change “Playground Settings -> Platform” from OS X to iOS
then you could import UIKit or some module for iOS
ps. I try to create a UIImageView but it doesn't show the correct image on the right side. It seem worthless to import UIKit
In Xcode 7, now you can't use the Quick Look to see the appearance of a UIView.
Instead, use the Assistant Editor and:
XCPlaygroundPage.currentPage.liveView = sampleView
Like this:
import XCPlayground
import UIKit
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
// Simulate User Interaction, not available in Xcode 7.2
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
let color = UIColor(red: 1, green: 1, blue: 0, alpha: 1)
let leftMargin = 20
let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667)) // iPhone 6 proportions
view.backgroundColor = UIColor.grayColor()
// LABEL
let label = UILabel(frame: CGRect(x: leftMargin, y: 5, width: 300, height: 44))
label.text = "Hello, playground"
label.textColor = UIColor.whiteColor()
view.addSubview(label)
// TEXTFIELD
let textField = UITextField(frame: CGRect(x: leftMargin, y: 60, width: 300, height: 44))
textField.placeholder = "Edit me…"
textField.backgroundColor = UIColor(white: 1, alpha: 0.5)
textField.textColor = UIColor.whiteColor()
textField.userInteractionEnabled = true
view.addSubview(textField)
XCPlaygroundPage.currentPage.liveView = view
delay(1.0) { () -> () in
textField.text = "New text!"
}
In Xcode 8 XCPlaygroundPage.currentPage.liveView is deprecated. Instead, use
import PlaygroundSupport
PlaygroundPage.current.liveView = view
Press CMD+Option+1 and change the platform to iOS, this will allow you to import UIKit.
I found I could add a new playground file in IOS project, and in that file I can import UIKit.
please use Command (⌘) + Option(⌥) + 1 combination to switch to iOS platform from OSX in playground to use UIKit .
Most simple solution:
Xcode 9.2
Start with a new Single View playground:
show Assistance Editor: View -> Assistance Editor -> Show Assistance Editor.
here you go. By default you will see printed property: label.text = "Hello World!" in the Live View window
Press Option+Cmd+1 and choose iOS in the Platform setting. Then you can import UIKit and play~
Yeah looks like it doesn't support UIkit yet.
Update:
When i wrote this answer in 2014 it didn't support UIKit, now it does. Leaving this for historical reference
Edit:
Actually above answer is incorrect.
You can create iOS project and add new .playground file inside that project. Then you can import Uikit or another iOS specific framework.

Resources