I've got a swift playground and I've created a UIButton called 'startButton'.
I have a function that sets up the parameters of the UIButton.
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonPressed), for: .touchUpInside)
view.addSubview(startButton)
}
I call this in the viewDidLoad
override func viewDidLoad() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766))
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
When I run the playground it doesn't show the button.
How do I show the button.
Thank you
You are setting PlaygroundPage.current.liveView to the local view created in viewDidLoad. This is not the same view of the view controller which is what you are adding the button to in the setupStartButton.
I would remove the line let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766)) then every reference to view will be the view controller's view.
You should remove the playground code from the viewDidLoad method. After the view controller class, you can create an instance of the view controller and setup the playground:
Something like:
class MyViewController: UIViewController {
// your code
}
PlaygroundPage.current.liveView = MyViewController()
Related
I’m new to swift and coding in general and I’m trying to make a truth or dare game but when I run the code using step through my code and press the red button it is running the code from Responder and not from responder1. I think the problem might be that I maid the 2nd button just by copying the for the 1st button and adding 1, and then swift thinks that the button 1 is the same as button and it runs the code for the 1st button.
import PlaygroundSupport
import UIKit
import SpriteKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.0, green: 0.7522757053375244, blue: 0.780606746673584, alpha: 1.0)
view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
lbl.textColor = .blue
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0)
//button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button.setTitle("button", for: UIControl.State.selected)
button.layer.cornerRadius = 10
view.addSubview(button)
let button1 = UIButton(frame: CGRect(x: 250, y: 100, width: 100, height: 50))
button1.backgroundColor = #colorLiteral(red: 1.0, green: 0.1893465518951416, blue: 0.1893465518951416, alpha: 1.0)
//button1.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button1.setTitle("button1", for: UIControl.State.selected)
button1.layer.cornerRadius = 10
view.addSubview(button1)
class Responser: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "aha"
}
}
let responder = Responser()
button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside)
class Responser1: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "UAh"
}
}
let responder1 = Responser()
button1.addTarget(responder1, action: #selector(Responser1.printname), for:.touchUpInside)
PlaygroundPage.current.liveView = view
I've got this UIButton called 'start button' in swift playgrounds. This is the setup for it.
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
I call this in viewDidLoad().
override func viewDidLoad() {
view.frame = CGRect(x: 0, y: 0, width: 524, height: 766)
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
But when I tap on a button it doesn't run the function called 'startButtonTapped' which just prints "Hello" to the console.
This is the function 'startButtonTapped'.
#objc func startButtonTapped() {
print("Hello")
}
Why does it not do anything when I tap the button?
How can I fix this?
Thank you
I can't see the rest of your code. I just quickly created one so that you could compare with your own code.
import UIKit
import PlaygroundSupport
class VC: UIViewController {
let startButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
setupStartButton()
}
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
}
#objc func startButtonTapped() {
print("Hello")
}
}
let vc = VC()
PlaygroundPage.current.liveView = vc
How i can add header just like the image below which should not scroll up with view controller.
To add static header view, use following function (Swift 4):
Note: I have tried to put this kind of header view directly using storyboard. But, it wont worked. So, finally I achieved it by adding programatically.
func addHeaderView() {
let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
selectableView.addSubview(btn)
let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
titleLabel.textColor = UIColor.white
titleLabel.text = "Winter event 2017"
selectableView.addSubview(titleLabel)
let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
subTitleLabel.textColor = UIColor.white
subTitleLabel.text = "135 members"
selectableView.addSubview(subTitleLabel)
let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnOptions.tag = 2
selectableView.addSubview(btnOptions)
view.addSubview(selectableView)
}
//then make a action method :
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
self.navigationController?.popViewController(animated: true)
} else {
askForSettings()
}
}
Also put this line of code in viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)
I have a tableView inside a navigationController inside a tabBarController.
I want to programatically fix a view above the tabbar like this:
.
I have experimented with adding constraints without success, also because I don't know whether to use self.view or self.bottomLayoutGuide of self.tableView or self.navigationController or self.tabBarController.tabBar or ... any suggestions?
Try this:
button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
button.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
button.tintColor = .white
button.titleLabel?.font = UIFont(name: "Helvetica", size: 20)
button.setTitle("YOUR TEXT", for: .normal)
button.addTarget(self, action: #selector(yourAction), for: .touchUpInside)
self.navigationController?.view.addSubview(button)
In my example, it is a UIButton but it can be a UIView too
Same example with UIView
let view = UIView(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
view.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
self.navigationController?.view.addSubview(view)
self.automaticallyAdjustsScrollViewInsets = NO;
then you will see the tableviewcell covered by navigationbar and
if you want navigationbar to be full transparency
self.navigationController.navigationBar.backgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault;
self.navigationController.navigationBar.shadowImage:[UIImage new];
Try this Swift 3.0
Bottom view code here.
let bottomview = UIView(frame: CGRect(x: 0, y: self.view.frame.height-63,width:self.view.frame.width, height: 80))
bottomview.backgroundColor = UIColor.red
self.view.addSubview(bottomview)
Table view code here...
let table: UITableViewController = MyTableViewController()
let tableView: UITableView = UITableView()
tableView.frame = CGRect(x: 0, y:self.navigationController.navigationBar.frame.size.height, width: self.view.frame.width, height: 500)
tableView.dataSource = table
tableView.delegate = table
self.view.addSubview(tableView)
I am working with the new beta version of the Playground app in iOS 10.
I want to get some UIKit elements working in a playground.
Swift 3 is just different enough from Swift 2, I am having some problems.
I got a label and text box working, but I cannot get a button working.
I have the button showing up but cannot get text in it or an action working on touch-up.
Here is my code I have.
import PlaygroundSupport
import UIKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
PlaygroundPage.current.liveView = view
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
let txt = UITextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50))
//txt.placeholder = "Enter text here"
//txt.font = UIFont.systemFont(ofSize: 15)
txt.borderStyle = UITextBorderStyle.roundedRect
view.addSubview(txt)
func buttonPressed(sender: UIButton)
{
//sender.backgroundColor = #colorLiteral(red: 0.725490212440491, green: 0.47843137383461, blue: 0.0980392172932625, alpha: 1.0)
}
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0)
button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.focused)
button.setTitle("button", for: UIControlState.focused)
button.layer.cornerRadius = 10
button.addTarget(button, action: "buttonPressed", for: UIControlEvents.touchUpInside)
view.addSubview(button)
I've been trying the same and the following code works for me:
import UIKit
import PlaygroundSupport
class Receiver {
#objc func buttonClicked() {
print("click")
}
}
let view = UIView()
view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let receiver = Receiver()
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 50))
button.setTitle("TestButton", for: .normal)
button.setTitleColor(#colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0), for: .normal)
button.addTarget(receiver, action: #selector(Receiver.buttonClicked), for: .touchUpInside)
PlaygroundPage.current.liveView = view
view.addSubview(button)
I think the important difference is that you add the button as target of the .touchUpInside event. Unfortunately, the button does not implement your buttonPressed method (it's just defined in the Playground, not as part of the UIButton class). As there is no self in a general playground (to refer to the function you wrote), I wrapped it into a new class and used an instance of this class to be the receiver of the .touchUpInside event.