This question already has answers here:
Add view over tableview (UITableViewController)
(7 answers)
Closed 5 years ago.
I have a table view controller and the only way for me is to using that so please don't tell me use viewController instead of tableviewController here is my codes for adding a button and this Button is shown in the last cell But I want when user scrolling table view the button be at the bottom of screen without moving
override func viewDidLoad() {
super.viewDidLoad()
let submitButton = UIButton(frame: CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
view.addSubview(submitButton)
Try this approach if it suits your scenario:
var submitButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
submitButton.frame = CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50)
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.window?.addSubview(submitButton)
}
But in this scenario you have to remove the button from superview in viewWillDisappear :
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self. submitButton.removeFromSuperview()
}
Try this
declare button as a variable
var submitButton:UIButton!
and in viewDidLoad
submitButton = UIButton(frame: CGRect(x: 5, y: UIScreen.main.bounds.size.height - 50 , width: UIScreen.main.bounds.size.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.addSubview(submitButton)
and implement this method
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
submitButton.frame = CGRect.init(x: submitButton.frame.origin.x, y: UIScreen.main.bounds.size.height + scrollView.contentOffset.y - 50 , width: submitButton.frame.width, height: submitButton.frame.height)
}
Related
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 to use the selector in Utility Class and there definition? My code create some error. PLease tell me how to resolve this error?
Error : My app crashed when I touched on any button.
class BarButton{
static func items(navigationItem: UINavigationItem) {
//Hide Back Button
navigationItem.hidesBackButton = true
//Add Label to Left of NavigationBar
let leftLabel = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
leftLabel.tintColor = UIColor.init(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setLeftBarButton(leftLabel, animated: false)
//List Button
let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
btnList.setImage(UIImage(named:"list-icn"), for: .normal)
btnList.addTarget(self, action: #selector(listBtn), for: .touchUpInside)
let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
//Notification Button
let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)
btnList2.addTarget(self, action: #selector(notificationBtn), for: .touchUpInside)
let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)
}
#objc func listBtn() {
print("List Btn")
}
#objc func notificationBtn() {
print("Notification Btn")
}
}
Try this and see:
let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
btnList.setImage(UIImage(named:"list-icn"), for: .normal)
// Update selector
btnList.addTarget(self, action: #selector(BarButton.listBtn(sender:)), for: .touchUpInside)
let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
//Notification Button
let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)
// Update selector
btnList2.addTarget(self, action: #selector(BarButton.notificationBtn(sender:)), for: .touchUpInside)
let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)
// Try using static
#objc static func listBtn(sender: Any)
{
print("List Btn")
}
// Try using static
#objc static func notificationBtn(sender: Any)
{
print("Notification Btn")
}
Your listBtn and notificationBtn methods are instance methods, but you try to invoke them on a class, not class instance - your items method is static, so self inside of it is of AnyObject.Type type.
To fix it, make listBtn and notificationBtn methods static.
I need some help over here. I try to learn... some things and practice but i'm not able to manage that function.
I have this function creating the container view:
func createGameView() {
let containerView = UIView()
containerView.frame = CGRect(x: 0,
y: self.screenHeight * 0.4,
width: screenWidth,
height: screenHeight * 0.6)
containerView.backgroundColor = UIColor(red:0.99, green:0.13, blue:0.00, alpha:1)
self.view.addSubview(containerView)
After that I have a function who will populate the container view with 9 squares
func createGridView( _ container : UIView) {
let buttonWidth = container.frame.size.width / 3
let buttonHeight = container.frame.size.height / 3
for i in [0,1,2] {
for j in 0...2 {
let button = UIButton(frame:
CGRect( x : buttonWidth * CGFloat (i),
y :buttonHeight * CGFloat (j),
width : buttonWidth,
height : buttonHeight ))
button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 1.0
container.addSubview(button)
button.addTarget(self, action: #selector(GameViewController.addGameValue(sender:)), for: .touchUpInside)
button.tag = i + 3 * j
self.gameButtons.append(button)
}
}
}
And I have a button called RESET
var resetButton = UIButton(frame:
CGRect(x: Double(containerView.frame.size.width - 100),
y: Double(containerView.frame.size.height - 60),
width: 60,
height: 40.0)
)
resetButton.setTitle ("Reset", for: UIControlState.normal)
resetButton.setTitleColor(UIColor.black, for: UIControlState.normal)
resetButton.setTitleColor(UIColor.white, for: .highlighted)
resetButton.layer.borderWidth = 1.0
resetButton.layer.borderColor = UIColor.black.cgColor
resetButton.layer.cornerRadius = 5.0
resetButton.addTarget(self, action: #selector(GameViewController.resetGame), for: UIControlEvents.touchUpInside)
containerView.addSubview(resetButton)
This is where i got a problem :(
func resetGame (sender: UIButton) {
// .addTarget(self, action: #selector(self.createGameView), for: .touchUpInside)
print(" reset me ")
// .addTarget(self, action: #selector(GameViewController.resetGame), for: .touchUpInside)
}
Anyone can tell me why none of those are not working to reset the value ? Is a tic tac toe game, and after put some x and 0 i want to restore the gridView to intial state. Got no ideea how to do it.
you already have all of the buttons that you need added to the view after createGridView is called, so on reset all you have to do is reset the button state, clear any scores/points and your ready to start again.
func resetGame() {
// reset any scores, colors etc
for button in self.gameButtons {
button.setTitle ("", for: UIControlState.normal)
button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
}
}
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.
I've builded a view which appears from the bottom and hides after sometime and it works well,but i want to make it in UIView class as a modal, i looked over the internet but i couldn't get or understand how to do that.
snake = UIView(frame: CGRect(x: 0 , y: self.view.frame.size.height-66, width: self.view.frame.size.width, height: 66))
snake.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
let label = UILabel(frame: CGRect(x: 12, y: 8, width: snake.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
snake.addSubview(label)
let button = UIButton(frame: CGRect(x: snake.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
snake.addSubview(button)
self.view.addSubview(snake)
How to start with this class I've no idea, i am creating the view programmatically with its frame and i need to set properties for the button for example or the label and create the view from any class.
class snake: UIView {
override init (frame : CGRect) {
super.init(frame : frame)
}
convenience init () {
self.init(frame:CGRect.zero)
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
}
code:
var label:UILabel!
var button:UIButton!
override init (frame : CGRect) {
super.init(frame : frame)
self.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
label = UILabel(frame: CGRect(x: 12, y: 8, width: self.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
self.addSubview(label)
button = UIButton(frame: CGRect(x: self.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(button)
}
and use it:
let snakeView = snake(frame: CGRectMake(0 ,self.view.frame.size.height-66, self.view.frame.size.width, 66)))
and set data for snakeview:
snakeView.label.text = "hello"
But usually i 'll create a function to update data for view:
func updateData(title:String){
self.label.text = title
}
and call it when need:
snake.updateData("hello")
P/s: if you use xib, you must to implement awakeFromNib instead of init. And create snake with xib(remember set identifier for xib : "snakeView"):
let snakeView = NSBundle.mainBundle().loadNibNamed("snakeView", owner: nil, options: nil)[0] as snakeView