Button to reload UIWebView - swift

I have created a button over top of a UIWebView that I would like to use to refresh the webview when pressed. The following code is in my ViewController class:
#IBOutlet weak var webView: UIWebView!
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
override func viewDidLoad() {
super.viewDidLoad()
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}
I have created the reload method to refresh the webView but nothing happens when I click the button. I also tried to create the reload method inside viewDidLoad to see if this fixed the issue. What could be causing this issue?

action: Selector(reload())
The syntax is wrong. On Xcode version 7.2 and below it should be
action: Selector("reload")
on Xcode 7.3 and up it should be
action: #selector(ClassName.funcName)
In Xcode 7.2 whole thing should be
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("reload"), forControlEvents: .TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}

You defined webview inside view did load scope, take it out:
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
webView.layer.zPosition = 1
self.view.addSubview(webView)
button.frame = CGRectMake(0, 0, 100, 100)
button.setTitle("Reload Page", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}

Related

#selector(didTapRegister) not in scope

When I run this code by #selector(didTapRegister) I get a didTapRegister is not in scope. I have a bigger application I am using it in and on one page it works and another it won't, well it won't work anywhere else.
I am stuck on figuring out why. Can anyone help solve it? seems to be an Objective-C issue possibly because I have same issues trying to make IBOutlets, I have tried searching for answers and I just don't have enough knowledge to understand why it isn't working, I've tried moving the code to different scopes and still nothing. with the IBOutlets it's saying IBOutlet property cannot have non-object type UIButton?.Type
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button = UIButton.init(type: .custom)
Button.setTitle("Chat", for: .normal)
Button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
Button.setTitleColor(.white, for: .normal)
Button.setTitleColor(.red, for: .highlighted)
Button.layer.borderWidth = 1
Button.layer.cornerRadius = 5
Button.layer.borderColor = UIColor.white.cgColor
Button.isHighlighted = false
Button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: Button)
Button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
}
Maybe try what he said but
Just use SwiftUI it is much better
#objc and all of that is fine but in SwiftUI you can just right click and add buttons
There 2 mistakes in the code.
You have to create #objc function for the button action
In your code move viewWillAppear method outside the viewDidLoad
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.init(type: .custom)
button.setTitle("Chat", for: .normal)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
button.isHighlighted = false
button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
#objc func didTapRegister(_ sender: UIButton) {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
Note: button name start with lowercase.

I need help streaming an online radio http://www.vdee.org:8000/salinas . I have this code so far

I need to stream an online radio station to put into an ios app using swift. Mine is running but not playing.
var player:AVPlayer?
var playerItem:AVPlayerItem?
var playButton:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let url = URL(string: "http://www.vdee.org:8000/salinasMim")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)
self.view.layer.addSublayer(playerLayer)
playButton = UIButton(type: UIButton.ButtonType.system) as UIButton
let xPostion:CGFloat = 50
let yPostion:CGFloat = 100
let buttonWidth:CGFloat = 150
let buttonHeight:CGFloat = 45
playButton!.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
playButton!.backgroundColor = UIColor.lightGray
playButton!.setTitle("Play", for: UIControl.State.normal)
playButton!.tintColor = UIColor.black
playButton!.addTarget(self, action: #selector(ViewController.playButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(playButton!)
}
#objc func playButtonTapped(_ sender:UIButton)
{
if player?.rate == 0
{
player!.play()
//playButton!.setImage(UIImage(named: "player_control_pause_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Pause", for: UIControl.State.normal)
} else {
player!.pause()
//playButton!.setImage(UIImage(named: "player_control_play_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Play", for: UIControl.State.normal)
}
}
I check your code. it's working fine with below VOD content and it should also work with DTV content.
https://cstr-vod.castr.io/vdafddac30977c11e9bdcb/71983cc5-f510-4b36-b77a-fc3d216e15d3/index.m3u8
http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
I am not able to get response from your URL: http://www.vdee.org:8000/salinasMim
Please use this URL:http://www.vdee.org:8000/salinas
this DTV content is working fine please check. You can listen the audio as the content in audio only.
var player:AVPlayer?
var playerItem:AVPlayerItem?
var playButton:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//https://cstr-vod.castr.io/vdafddac30977c11e9bdcb/71983cc5-f510-4b36-b77a-fc3d216e15d3/index.m3u8
//http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
let url = URL(string: "http://www.vdee.org:8000/salinas")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)
self.view.layer.addSublayer(playerLayer)
playButton = UIButton(type: UIButton.ButtonType.system) as UIButton
let xPostion:CGFloat = 50
let yPostion:CGFloat = 100
let buttonWidth:CGFloat = 150
let buttonHeight:CGFloat = 45
playButton!.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
playButton!.backgroundColor = UIColor.lightGray
playButton!.setTitle("Play", for: UIControl.State.normal)
playButton!.tintColor = UIColor.black
playButton!.addTarget(self, action: #selector(ViewController.playButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(playButton!)
}
#objc func playButtonTapped(_ sender:UIButton)
{
if player?.rate == 0
{
player!.play()
//playButton!.setImage(UIImage(named: "player_control_pause_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Pause", for: UIControl.State.normal)
} else {
player!.pause()
//playButton!.setImage(UIImage(named: "player_control_play_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Play", for: UIControl.State.normal)
}
}

Why Doesn't Swift 3 recognize me button tap?

let playButton: UIButton = {
let button = UIButton()
let image = UIImage(named: "VideoIcon.png") as UIImage?
button.backgroundImage(for: .normal)
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
button.setImage(image, for: .normal)
return button
}()
func pressBackButton(button: UIButton) {
print("test")
if let playVideoButtonURL = post?.videourl {
let player = AVPlayer(url: playVideoButtonURL as URL)
let playerLayer = AVPlayerLayer(player:player)
playerLayer.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.layer.addSublayer(playerLayer)
player.play()
}
}
even if the video code is wrong it should still print test but it doesn't which is strange. I feel like something may be wrong with the selector but i currently have no idea what could be wrong with it.
access function or property in self outside of initializer.
class TestViewController: UIViewController {
let playButton: UIButton = {
let button = UIButton()
let image = UIImage(named: "VideoIcon.png") as UIImage?
button.backgroundImage(for: .normal)
button.setImage(image, for: .normal)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
}
}
Instead of doing:
let playButton: UIButton = {
...
...
return button
}()
which won't work (because you're attempting to create playButton at the wrong time -- which is before the view is loaded or before it's about to appear)
In your viewDidLoad or viewWillAppear, create the button, define the target and simply add it as a subview.
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
if let image = UIImage(named: "VideoIcon.png") as UIImage?
{
button.backgroundImage(for: .normal)
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
button.setImage(image, for: .normal)
}
self.addSubview(button)
}
Adding the button as a subview increments the retain count (i.e. it won't go away), but if you want to keep it around as a property simply declare it as:
var playButton : UIButton?
and then set playButton = button at the end of viewDidLoad.

Button triggers another view

Right now I have this:
lazy var Button: UIButton = {
let tb = UIButton()
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
tb.setBackgroundImage(UIImage(named: "buttonMenu"), forState: UIControlState.Normal)
return tb
}()
How can I make the button take me to another view or do other stuff like opening the camera?
PS: The showButton function doesn't do anything really.
So you have
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
but you need
tb.addTarget(self, action: #selector(className.showButton), forControlEvents: .TouchUpInside)
func showButton(sender:UIButton) {
//Code
}
I have tried this in 7.2.1 Xcode
import UIKit
import CoreLocation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button: UIButton = {
let tb = UIButton()
tb.frame = CGRectMake(0, 0, 100, 50)
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: "showButton:", forControlEvents: .TouchUpInside)
// only change the action syntax like "action: "showButton:" "
return tb
}()
self.view .addSubview(Button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showButton(sender:AnyObject?) {
//Code
}
}

Swift custom back button in navigation bar

I am trying to use a custom image for my back button in the navigation bar. I am using the below code, which adds the image, but also keeps the text "Back" in the button. I want to also remove the text. Can I do that?
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "icon-back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "icon-back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
Try code below :-)
func SetBackBarButtonCustom()
{
//Back buttion
let btnLeftMenu: UIButton = UIButton()
btnLeftMenu.setImage(UIImage(named: "back_arrow"), for: UIControlState())
btnLeftMenu.addTarget(self, action: #selector(UIViewController.onClcikBack), for: UIControlEvents.touchUpInside)
btnLeftMenu.frame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func onClcikBack()
{
_ = self.navigationController?.popViewController(animated: true)
}
If you want to add Back button in every UIViewController then you can add the code in UIViewController extension else you can use addBackButton() directly as follows.
extension UIViewController {
func addBackButton() {
let btnLeftMenu: UIButton = UIButton()
let image = UIImage(named: "backButtonImage");
btnLeftMenu.setImage(image, for: .normal)
btnLeftMenu.setTitle("Back".localized, for: .normal);
btnLeftMenu.sizeToFit()
btnLeftMenu.addTarget(self, action: #selector (backButtonClick(sender:)), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func backButtonClick(sender : UIButton) {
self.navigationController?.popViewController(animated: true);
}
}
Make sure you should add the following file "backButtonImage.png" in your app bundle.
Call this method self.addBackButton()in your viewDidLoad method of your custom UIViewController class like this
override func viewDidLoad() {
super.viewDidLoad()
self.addBackButton()
}
Note : if you don't add addBackButton Method in extension then you will need to add this method directly in the class and set target and Selector accordingly.