【问题标题】:How to add Spinner Indicator on Programmatically created Button如何在以编程方式创建的按钮上添加微调器指示器
【发布时间】:2016-03-04 13:20:27
【问题描述】:

你好,我已经像这样在我的静态 TableView 上动态创建了按钮

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
}

我想在单击按钮时在按钮顶部显示微调器。我知道如何制作点击功能或如何创建微调器。我只是不知道如何将微调器放在按钮顶部代替标题,以便当用户单击按钮时,标题隐藏并且微调器在标题位置移动。我希望你明白我在说什么

【问题讨论】:

  • 将微调器添加为按钮的子视图。将微调器的中心设置为 button.center 和:[button addSubview:yourspinner];

标签: ios swift uibutton uiactivityindicatorview


【解决方案1】:
UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [myspinner setCenter:button.center];
    [button addSubview:myspinner];

【讨论】:

    【解决方案2】:

    这是我的快速版本

    let loginSpinner: UIActivityIndicatorView = {
        let loginSpinner = UIActivityIndicatorView(activityIndicatorStyle: .white)
        loginSpinner.translatesAutoresizingMaskIntoConstraints = false
        loginSpinner.hidesWhenStopped = true
        return loginSpinner
    }()
    

    然后在我的viewDidLoad函数中:

        loginButton.addSubview(loginSpinner)
        loginSpinner.centerXAnchor.constraint(equalTo: loginButton.centerXAnchor).isActive = true
        loginSpinner.centerYAnchor.constraint(equalTo: loginButton.centerYAnchor).isActive = true
    

    最后在需要的地方调用loginSpinner.startAnimating()loginSpinner.stopAnimating() 函数。

    注意:开始和停止动画时,我也会禁用按钮,因此取消设置禁用按钮的标题,以便微调器替换标题 loginButton.setTitle("", for: .disabled) // clear the title when it is disabled to just show the spinner

    【讨论】:

      【解决方案3】:
      1. 您创建一个微调器 (UIActivityIndicatorView),同时使其自动隐藏 (setHidesWhenStopped:)
      2. 您将其作为子视图添加到您的按钮 (addSubview)
      3. 您将其放在按钮的中心 (setCenter:)
      4. 按下 On 按钮,将标题设置为空字符串 (setTitle:forControlState:) 并运行微调器 (startAnimating)

      【讨论】:

        【解决方案4】:

        这是我通常的做法,您可以利用一些不同的行为:

        let spinner = UIActivityIndicatorView(activityIndicatorStyle: .White)
        
        spinner.frame = CGRect(x: -20.0, y: 6.0, width: 20.0, height: 20.0) // (or wherever you want it in the button)
        spinner.startAnimating()
        spinner.alpha = 0.0
        
        button.addSubview(spinner)
        

        您可以相应地更改 alpha。或者使用隐藏属性、停止/开始动画等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-13
          • 2015-06-30
          • 2011-02-16
          • 1970-01-01
          • 1970-01-01
          • 2016-03-06
          相关资源
          最近更新 更多