Macos Playground: How Can Change the Background Color

MacOS Playground: How can change the background color?

You just need to set your view wantsLayer property to true

let view = views[0] as! NSView
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.blue.cgColor

Sample Image

Center a view inside viewcontroller in iOS Playground app

You can try to do it using Autolayout:

let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
newView.leftAnchor.constraint(equalTo: view.leftAnchor),
newView.rightAnchor.constraint(equalTo: view.rightAnchor),
newView.widthAnchor.constraint(equalTo: newView.heightAnchor),
])
newView.backgroundColor = UIColor.red

UPDATE

Playground code:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .red
view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
self.view = view

let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
newView.backgroundColor = .blue
view.addSubview(newView)

newView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
// need to define its size too
newView.heightAnchor.constraint(equalToConstant: 375),
newView.widthAnchor.constraint(equalTo: view.heightAnchor),
])
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

Xcode 9 Swift 4 Playgrounds UIGestureRecognizer not working

You have used incorrect values of height for your card views.

Changing height value to a lesser value of 47 worked:

cardViewOne.frame = CGRect(x: 0, y: 218, width: 375, height: 47)
cardViewTwo.frame = CGRect(x: 0, y: 164, width: 375, height: 47)
cardViewThree.frame = CGRect(x: 0, y: 109, width: 375, height: 47)

Also, because cardButtonTwo is a UIButton, you don't need to use UITapGestureRecognizer. You can simply add target for touchUpInside event.

override func viewDidLoad() {
cardButtonTwo.addTarget(self, action: #selector(HomeViewController.handleTap(sender:)), for: .touchUpInside)
self.navigationController?.isNavigationBarHidden = true
}

iOS Playground doesn't show UI preview

Open the preview panel: View > Assistant Editor > Show Assistant Editor

Then in your code:

import PlaygroundSupport
PlaygroundPage.current.liveView = view

Don't forget to give your view a visible frame.


Ps: after Xcode 9, you can create a playground with default view

playground

IOS Background Gradients - Storyboard

I recommend trying the code in a swift playground. Swift Playground executes your code every time you type. You can see what your background looks like and edit it however you wish. Then you can paste your code into your Xcode project without having to run the simulator.

Good luck

Does swift playground support UIKit?

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.

ios playground quickview

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.
ios playground example

(EDIT: minor text update to current CGRect syntax -- But, screenshots still show old syntax.)

UISegmentedControl iOS 13 clear color

Here's a way to replicate that plain segmented control in iOS 13:

import UIKit
import PlaygroundSupport

class PlainSegmentedControl: UISegmentedControl {
override init(items: [Any]?) {
super.init(items: items)

setup()
}

override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// Used for the unselected labels
override var tintColor: UIColor! {
didSet {
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
}
}

// Used for the selected label
override var selectedSegmentTintColor: UIColor? {
didSet {
setTitleTextAttributes([.foregroundColor: selectedSegmentTintColor ?? tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}

private func setup() {
backgroundColor = .clear

// Use a clear image for the background and the dividers
let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: 32))
setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)

// Set some default label colors
setTitleTextAttributes([.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}

Here's some test code to put in a playground:

// Create a dark green view as a test background
let bg = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100))
bg.backgroundColor = UIColor(red: 0.224, green: 0.408, blue: 0.467, alpha: 1)

// The plain segmented control
let seg = PlainSegmentedControl(items: ["Number One", "Number Two", "Number Three"])
seg.tintColor = .white
seg.selectedSegmentTintColor = .green
seg.selectedSegmentIndex = 0
bg.addSubview(seg)
PlaygroundPage.current.liveView = bg

Here's the UIImage extension to create a sized image from a color:

extension UIImage {
convenience init(color: UIColor, size: CGSize) {
UIGraphicsBeginImageContextWithOptions(size, false, 1)
color.set()
let ctx = UIGraphicsGetCurrentContext()!
ctx.fill(CGRect(origin: .zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

self.init(data: image.pngData()!)!
}
}

Subview not working in xcode playground

You are doing so many things wrong :(

This will run in a playground page, and should be close to what you're going for. Check the comments to understand what's going on:

import UIKit
import Foundation
import PlaygroundSupport

class SecondView : UIView{

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

func commonInit() {

// create a label
let label = UILabel()
// we're going to use auto-layout
label.translatesAutoresizingMaskIntoConstraints = false

// add the label to self
self.addSubview(label)

// pin the label to leading, trailing and bottom -- all to Zero
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0).isActive = true
label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0.0).isActive = true
label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true

label.textAlignment = .center
label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
label.text = "Hejsan"
label.textColor = .white

}

}

class MyViewController : UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// we don't need to create a new view
//let view = UIView()

view.backgroundColor = .green

// create the "SecondView"
let sView = SecondView()
// we're going to use auto-layout
sView.translatesAutoresizingMaskIntoConstraints = false

// add the new view
view.addSubview(sView)

// pin the new view to top, leading and trailing -- all to Zero so it fills this view
sView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
sView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
sView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
sView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true

sView.backgroundColor = .red

}

}

// Present the view controller in the Live View window

let vc = MyViewController()
vc.preferredContentSize = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc

Result:

Sample Image



Related Topics



Leave a reply



Submit