Swift 3 - Add Text to UIView Pop up - ios

I want to display a pop up with some fixed text in it.
I found this for the pop up.
// Define a view
var popup:UIView!
func showAlert() {
// customise your view
popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
popup.backgroundColor = UIColor.redColor
// show on screen
self.view.addSubview(popup)
// set the timer
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.dismissAlert), userInfo: nil, repeats: false)
}
func dismissAlert(){
if popup != nil { // Dismiss the view from here
popup.removeFromSuperview()
}
}
I can't seem to add a label in the showAlert() and display it.
I tried calling a seperate function in showAlert that gets the label text, that works but it wont dismiss.
How can I add a text/string/Label to the pop up in the showAlert function itself? I want to use UIView itself, not AlertController.

You can try
popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
let lb = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
lb.text="anything"
popup.backgroundColor = UIColor.redColor
// show on screen
self.view.addSubview(popup)
popup.addSubview(lb)
lb.center = popup.center

Related

swift pan a imageView that is behind a view

Is it possible to pan an object that is behind another object?
Currently I have a imageview and a view with clear color and only a border around, that I can see the imageview. I want to achieve that the view with the border is always on top. Trough the view I want to pan the image view with pan gesture.
You can add Pan gesture to the view and apply the transformation to the imageView.
var translation = panGesture.translationInView(imageView)
panGesture.setTranslation(CGPointZero, inView: imageView)
Just set isUserInteractionEnabled false for upper view.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let bottomView = UIView.init(frame: .init(x: 100, y: 100, width: 300, height: 300))
bottomView.backgroundColor = .yellow
let upperView = UIView.init(frame: .init(x: 100, y: 100, width: 300, height: 300))
upperView.backgroundColor = .clear
upperView.layer.borderWidth = 1
upperView.layer.borderColor = UIColor.red.cgColor
upperView.isUserInteractionEnabled = false //<------------
view.addSubview(bottomView)
view.addSubview(upperView)
bottomView.addGestureRecognizer(UIPanGestureRecognizer.init(target: self, action: #selector(handle)))
}
#objc func handle() {
print("handletapgesture")
}
}

object value doesn't change after didset

I am using some cocoa pods frame to make UICollectionView. I am able to get the right data for each cell and setup views.
I want to have different ani
The animation end point should be related to the value of dataSource.The problem is that I can't use the didSet value as I wish. The frame of subviews I added are (0,0,0,0), the values of objects keep the same as initial value outside the didSet. I am not able to access the dataSource Value in setupView function.
Expected effect is that the red bar will move from left side to the right position:
class CereCell : DatasourceCell {
override var datasourceItem: Any?{
didSet {
guard let cere = datasourceItem as? CeremonyDay else { return }
nameLabel.text = cere.name
nameLabel.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
currentBar.frame = CGRect(x: 100, y: 100, width: cere.percentage*100 , height: 10)
position.x = cere.percentage
}
}
let currentBar: UIView = {
let currentBar = UIView(frame: CGRect(x: 290, y: 100, width: 300, height: 10))
currentBar.backgroundColor = .red
return currentBar
}()
override func setupViews() {
super.setupViews()
contentView.addSubview(currentBar)
let frame = currentBar.frame
print(frame)
It just print the (290, 100, 300, 10), which is not the value in didSet.

center an indicator view in an alertcontroller programmatically

I am trying to center an activity indicator in an alert controller programmatically but am not seeing the expected outcome.
Attempt:
import NVActivityIndicatorView
public func displayActivityAlertWithCompletion(ViewController: UIViewController, pending: UIAlertController, completionHandler: #escaping ()->())
{
//create an activity indicator
let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))
//indicator.clipsToBounds = true
indicator.type = .ballScaleMultiple
indicator.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]
indicator.color = UIColor(rgba: Palette.loadingColour)
//add the activity indicator as a subview of the alert controller's view
pending.view.addSubview(indicator)
indicator.isUserInteractionEnabled = false
// required otherwise if there buttons in the UIAlertController you will not be able to press them
indicator.startAnimating()
ViewController.present(pending, animated: true, completion: completionHandler)
}
The problem you are facing lies in this line:
let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))
At this point your pending alert controller is not yet presented, and it's frame is not updated.
You will have to update the frame of the indicator after the alert controller was presented, so for example you can do the following.
Replace this line:
ViewController.present(pending, animated: true, completion: completionHandler)
With this:
ViewController.present(pending, animated: true, completion: {
// update frame here:
indicator.frame = CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50)
// and manually call completionHandler:
completionHandler()
})

KDCircularProgressView not showing

I want to make KDCircularProgressView programmatically. I included KDCircularProgressView.swift file from https://github.com/kaandedeoglu/KDCircularProgress.
Here is code form UIViewController:
private var progressView: KDCircularProgress!
private func setupProgressView(){
progressView = KDCircularProgress(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
progressView.startAngle = 90
progressView.angle = 90
progressView.trackColor = UIColor.red
progressView.progressColors = [UIColor.white]
progressView.center = view.center
}
I am calling this function in viewDidLoad, but nothing is showing on screen.
add it to main view
self.view.addSubview(progressView)

UIActivityIndicatorView dialog over view

In my app I'm using a custom UIActivityIndicator view (this) and it works great.
I have an UITableView and I want the indicator over the tableview on loading data. To solve the problem I have created an empty view with a background above all and when I need to start indicator's animation I simply hide tableview and show the empty view + the indicator.
The result is this:
I saw somewhere that I can use instead something like this:
How can I do?
Thanks in advance.
You could try adding a translucent Black view over the Table View and put the custom activity indicator code as a subview in the Black view.
let aBlackView = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height))
aBlackView.backgroundColor = UIColor.blackColor()
aBlackView.alpha = 0.75 // Change the alpha depending on how much transparency you require
let aMainWindow = UIApplication.sharedApplication().delegate!.window
aMainWindow!!.addSubview(aBlackView)
/* Enter you code for NVActivityIndicatorViewable here */
aBlackView.addSubview(/* NVActivityIndicatorViewable */)
Once you are done with your request, you can remove the Black view by calling this code:
aBlackView.removeFromSuperview()
I created a activity showing screen . I used in every where in my project.
//ProgressBarNotification.swift
import Foundation
import UIKit
class ProgressBarNotification {
internal static var strLabel = UILabel()
internal static var messageFrame = UIView()
internal static var activityIndicator = UIActivityIndicatorView()
internal static func progressBarDisplayer(msg:String,indicator:Bool ){
let view1 = UIApplication.sharedApplication().delegate?.window!!.rootViewController?.view
view1?.userInteractionEnabled = false
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
messageFrame = UIView(frame: CGRect(x: view1!.frame.midX - 90, y: view1!.frame.midY - 25 , width: 180, height: 50))
messageFrame.layer.cornerRadius = 15
messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
if indicator {
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
messageFrame.addSubview(activityIndicator)
}
messageFrame.addSubview(strLabel)
view1!.addSubview(messageFrame)
}
internal static func removeProgressBar() {
let view1 = UIApplication.sharedApplication().delegate?.window!!.rootViewController?.view
_ = view1?.subviews.filter({ (view) -> Bool in
if view == ProgressBarNotification.messageFrame {
view.removeFromSuperview()
}
return false
})
ProgressBarNotification.messageFrame.removeFromSuperview()
view1?.userInteractionEnabled = true
}
deinit{
}
}
Usage
//To start Loader
ProgressBarNotification.progressBarDisplayer("Loading", indicator: true)
//Stop
ProgressBarNotification.removeProgressBar()

Resources