iosswiftuibuttonuiactivityindicatorview

How to add Spinner Indicator on Programmatically created Button


hello I have created button dynamically on my Static TableView like this

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height))

  let button   = UIButton(type: UIButtonType.System) as UIButton
        button.frame = CGRectMake(0, 0, 414, 65)

        button.setTitle(buttonTitle, forState: UIControlState.Normal)
        button.addTarget(self, action:buttonAction, forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
        button.titleLabel?.font = UIFont(name: Variables.MONTESERRAT_REGULAR, size: 20.0)
  button.backgroundColor = UIColor().blueColor()       //top
         footerView.addSubview(button!)


        return footerView
}

I want to show spinner on top of button when Its clicked. I know how to make the click function or how to create a spinner. I just don't know how to place the spinner on top of button in place of title so that when user clicks the button, title hides and spinner moves on title place. I hope you understand what I am saying


Solution

  • UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [myspinner setCenter:button.center];
        [button addSubview:myspinner];