How can add black background behind the UIActivityIndicatorView - ios

Hello I want to show a UIActivityIndicatorView on my UIViewController exactly like this
How can I draw a UIViewIndicator like this above image
I have tried this
func showIndicatorView(){
let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
loadingIndicator.layer.cornerRadius = 05
loadingIndicator.opaque = false
loadingIndicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
loadingIndicator.center = self.view.center;
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.color = UIColor.whiteColor()
loadingIndicator.startAnimating()
self.view.addSubview(loadingIndicator)
}

You could put it in another view that has a black background color. Something like this. You could also add the 'Loading...' label in that view if you need it.
func showIndicatorView(){
let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
let backgroundView = UIView()
backgroundView.layer.cornerRadius = 05
backgroundView.clipsToBounds = true
backgroundView.opaque = false
backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.color = UIColor.whiteColor()
loadingIndicator.startAnimating()
let loadingLabel = UILabel()
loadingLabel.text = "Loading..."
let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ])
loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height)
loadingLabel.center.y = loadingIndicator.center.y
backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50)
backgroundView.center = self.view.center;
self.view.addSubview(backgroundView)
backgroundView.addSubview(loadingIndicator)
backgroundView.addSubview(loadingLabel)
}

Use MBProgressHud library .
This is what you need :-
https://github.com/jdg/MBProgressHUD

Related

UINavigationItem is behind UINavigationBar

I've set up a UINavigationBar to have rounded bottom corners with shadow, but it removed my UINavigationItem. I've tried to set it back programatically but it sets it behind the top bar item.
class RoundedShadowCorners {
func shadowTopBar(_ topBar: UINavigationBar,_ offset: CGFloat,_ navigationItem: UINavigationItem){
topBar.isTranslucent = false
topBar.tintColor = UIColor.orange
topBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
topBar.shadowImage = UIImage()
topBar.backgroundColor = UIColor.white
let shadowView = UIView(frame: CGRect(x: 0, y: -offset, width: (topBar.bounds.width), height: (topBar.bounds.height) + offset))
shadowView.backgroundColor = UIColor.white
topBar.insertSubview(shadowView, at: 1)
let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: shadowView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
shadowLayer.fillColor = UIColor.white.cgColor
shadowLayer.shadowColor = UIColor.darkGray.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
shadowLayer.shadowOpacity = 0.8
shadowLayer.shadowRadius = 2
shadowView.layer.insertSublayer(shadowLayer, at: 0)
topBar.prefersLargeTitles = true
topBar.topItem?.title = "HJFSKDJKA"
}
}
offset is view.safeAreaInsets.top! Picture attached, as you can see, the title is behind the layer.
Text is behind
As you can see, layer works
I have edited #Daljeet's code and thanks to that I've found the solution:
let titleLabelView = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
titleLabelView.backgroundColor = UIColor.clear
titleLabelView.textAlignment = .center
titleLabelView.textColor = UIColor.black
titleLabelView.font = UIFont.systemFont(ofSize: 17)
titleLabelView.adjustsFontSizeToFitWidth = true
titleLabelView.text = navigationItem.title
let labelView: UIView = titleLabelView
topBar.insertSubview(labelView, aboveSubview: shadowView)
let yCoordPlaceholder = labelView.center.y
labelView.center = CGPoint(x: safeArea.width/2, y: yCoordNavPlaceholder)
Thanks again #Daljeet!
You can try below code:
// First create custom label
UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[titleLabelView setBackgroundColor:[UIColor clearColor]];
[titleLabelView setTextAlignment: NSTextAlignmentCenter];
[titleLabelView setTextColor:[UIColor whiteColor]];
[titleLabelView setFont:[UIFont systemFontOfSize: 27]];
[titleLabelView setAdjustsFontSizeToFitWidth:YES];
titleLabelView.text = #"You text here";
// here set in navBar titleView
topBar.topItem.titleView = titleLabelView;
topBar.bringSubviewToFront(topBar.topItem!.titleView!)
Add this line of code after setting the title.

Not able to position UIActivityIndicatorView correctly

I added an UIActivityIndicatorView to a scene, and it worked fine. Then I tried to add a small container and show the activity indicator on top of that.
This is the code I have.
func showActivityIndicator(uiView: UIView) {
let container: UIView = UIView()
container.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0);
container.center = uiView.center
container.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
let activityIndicator = UIActivityIndicatorView()
activityIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0);
activityIndicator.center = uiView.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
container.addSubview(activityIndicator)
uiView.addSubview(container)
activityIndicator.startAnimating()
}
The container view is displayed in the middle of the screen, but there is no activity indicator. When I check the debug view hierarchy, I see the activity indicator is outside the view, in the lower right corner. Can any one please explain why it ended up there after I added the container? And how can I position it correctly?
Thanks.
there is an issue with below code
activityIndicator.center = uiView.center
please try this
activityIndicator.center = CGPoint(x: container.frame.size.width/2,y: container.frame.size.height/2)
This will work:
func showActivityIndicator(uiView: UIView) {
let container: UIView = UIView()
container.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0);
container.center = uiView.center
container.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
let activityIndicator = UIActivityIndicatorView()
activityIndicator.center = container.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
container.addSubview(activityIndicator)
uiView.addSubview(container)
activityIndicator.startAnimating()
}

Center a UILabel of a UIView

I am currently trying to make a simple pop up, here I have a button, once it gets tapped.
It should show a simple view above it, I achieved the blue background, but I am not being able to add a label to it, and center the label to the blue popup
let width = (sender as! UIButton).frame.width
let y = (sender as! UIButton).frame.origin.y
let x = (sender as! UIButton).frame.origin.x
myView = UIView(frame: CGRect(x: x, y: y - 30, width:width, height: 30))
myView.backgroundColor = UIColor.blue
let label = UILabel()
label.text = (sender as! UIButton).titleLabel?.text
label.font = UIFont(name:"avenirNext-Meduim",size:20)
label.center = self.myView.center
myview.addSubview(label)
self.view.addSubview(myView)
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations: {
self.myView.alpha = 0.0
}) { (finished: Bool) in
self.myView.isHidden = true
}
Try to set your label frame:
let label = UILabel(frame: CGRect(origin: .zero, size: yourLabelSize))
Instead of
label.center = self.myView.center
try
label.centerXAnchor.constraint(equalTo: myView.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
after
myview.addSubview(label)
self.view.addSubview(myView)
My solution
let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: myView.center.x, y: myView.center.y, width: 51, height: 30)
codedLabel.textAlignment = .center
codedLabel.text = "q"
codedLabel.numberOfLines = 1
codedLabel.textColor=UIColor.red
codedLabel.font=UIFont.systemFont(ofSize: 22)
codedLabel.backgroundColor=UIColor.lightGray
self.myView.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.centerYAnchor.constraint(equalTo: codedLabel.superview!.centerYAnchor).isActive = true
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
let label = UILabel()
label.frame = CGRect.init(x: headerView.center.x-60, y:headerView.center.y-15, width: 120, height: 30)
label.textAlignment = .center
label.textColor = #colorLiteral(red: 0.1290470958, green: 0.2743584514, blue: 0.6418049335, alpha: 1)
label.font = UIFont(name: "Roboto-Bold", size: 15)
label.text = "11-12-2020"
headerView.addSubview(label)
headerView.backgroundColor = #colorLiteral(red: 0.9967060685, green: 0.9998621345, blue: 0.9997505546, alpha: 1)

i cant see the viewUtils.showActivityIndicator in my ios app

i need a show activityindicator in my app, but i cant see it. I have my viewutils (of type viewcontrollerultis) and i call it this way: viewUtils.showActivityIndicator(self.view). i dont know why this happen.
let viewUtils = ViewControllerUtils()
#IBAction func login(sender: AnyObject) {
viewUtils.showActivityIndicator(self.view)
username = userField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
password = passwordTextField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (self.username.isEmpty || self.password.isEmpty){
self.showAlert("Asegurese de ingresar un usuario y contraseƱa!")
}else{
var user = user_function()
if user.user_valid(username,password: password){
}else{
self.showAlert("Usuario Invalido")
}
}
viewUtils.hideActivityIndicator(self.view)
}
and this is my showactivityindicator:
func showActivityIndicator(uiView: UIView) {
container.frame = uiView.frame
container.center = uiView.center
container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = uiView.center
loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);
loadingView.addSubview(activityIndicator)
container.addSubview(loadingView)
uiView.addSubview(container)
activityIndicator.startAnimating()
}

iOS Badge like UILabel glitch

please help me to make a simple badge like UILabel.
My code is:
let badgeLabel = UILabel(frame: CGRectMake(0, 0, 25, 25))
badgeLabel.backgroundColor = UIColor.clearColor()
badgeLabel.layer.backgroundColor = UIColor.redColor().CGColor
badgeLabel.layer.cornerRadius = 25/2
badgeLabel.layer.borderWidth = 3.0
badgeLabel.layer.borderColor = UIColor.whiteColor().CGColor
And in result I have a UILabel with tiny red stroke on white border:
Try this.
let badgeLabel = UILabel(frame: CGRectMake(0, 0, 25, 25))
badgeLabel.backgroundColor = UIColor.redColor()
badgeLabel.layer.backgroundColor = UIColor.clearColor().CGColor
badgeLabel.layer.cornerRadius = 25/2
badgeLabel.layer.borderWidth = 3.0
badgeLabel.layer.borderColor = UIColor.whiteColor().CGColor
Hey Check this latest code, sure it will work.
let roundRing = UILabel(frame: badgeLabel.frame)
roundRing.backgroundColor = UIColor.clearColor()
roundRing.layer.backgroundColor = UIColor.whiteColor().CGColor
roundRing.layer.cornerRadius = 25/2
roundRing.layer.borderWidth = 3.0
roundRing.layer.borderColor = UIColor.whiteColor().CGColor
self.view .addSubview(roundRing)
let innerRegion = UILabel(frame: CGRectMake(3, 3, 19, 19))
innerRegion.backgroundColor = UIColor.clearColor()
innerRegion.layer.backgroundColor = UIColor.redColor().CGColor
innerRegion.layer.cornerRadius = 19/2
innerRegion.text="2"
innerRegion.font=UIFont(name: "MarkerFelt-Thin", size: 10)!
innerRegion.textAlignment=NSTextAlignment.Center
roundRing.addSubview(innerRegion)
You are missing masksToBounds
for swift 3:
let badgeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
badgeLabel.backgroundColor = .red
badgeLabel.layer.cornerRadius = 25/2
badgeLabel.layer.masksToBounds = true
badgeLabel.layer.borderWidth = 3.0
badgeLabel.layer.borderColor = .white

Resources