xcode 导航栏中心的活动指示器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16057034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Activity indicator at the centre of Navigation Bar
提问by Jeff
In my application I want add activity indicator at the centre of navigation bar(title position).when web service response completed it should replace with the old title.I have 5 navigation bars in my application.When I searched in google I got several codes but they are simply changing the left or right bar button.Any help ?
在我的应用程序中,我想在导航栏(标题位置)的中心添加活动指示器。当 Web 服务响应完成时,它应该替换为旧标题。我的应用程序中有 5 个导航栏。当我在谷歌搜索时,我得到了几个代码但他们只是在更改左侧或右侧栏按钮。有帮助吗?
回答by rdelmar
You use the titleView property of the navigation item to replace the title of a navigation bar. So to add an activity indicator, just do this:
您可以使用导航项的 titleView 属性来替换导航栏的标题。因此,要添加活动指示器,只需执行以下操作:
UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
aiView.hidesWhenStopped = NO; //I added this just so I could see it
self.navigationItem.titleView = aiView;
When you want to remove it, and show the title again:
当你想删除它时,再次显示标题:
self.navigationItem.titleView = nil;
回答by Waffeln
pasqls answer worked well for me, i've written it in swift
pasqls 的答案对我来说效果很好,我很快就写好了
func showActivityIndicator() {
let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicatorView.frame = CGRectMake(0, 0, 14, 14)
activityIndicatorView.color = UIColor.blackColor()
activityIndicatorView.startAnimating()
let titleLabel = UILabel.new()
titleLabel.text = "...Connecting"
titleLabel.font = UIFont.italicSystemFontOfSize(14)
let fittingSize = titleLabel.sizeThatFits(CGSizeMake(200.0, activityIndicatorView.frame.size.height))
titleLabel.frame = CGRectMake(activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8, activityIndicatorView.frame.origin.y, fittingSize.width, fittingSize.height)
let titleView = UIView(frame: CGRectMake(((activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width) / 2), ((activityIndicatorView.frame.size.height) / 2), (activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width), (activityIndicatorView.frame.size.height)))
titleView.addSubview(activityIndicatorView)
titleView.addSubview(titleLabel)
self.navigationItem.titleView = titleView
}
func hideActivityIndicator() {
self.navigationItem.titleView = nil
}
回答by pasql
Also, if you want to add a text label next to the activity indicator (as done by Apple in the settings app, e.g. Facebook Login), you can do this:
此外,如果您想在活动指示器旁边添加文本标签(如 Apple 在设置应用程序中所做的那样,例如 Facebook 登录),您可以这样做:
- (void)showActivityIndicator
{
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicatorView.frame = CGRectMake(0, 0, 22, 22);
activityIndicatorView.color = [UIColor blackColor];
[activityIndicatorView startAnimating];
UILabel *titleLabel = [UILabel new];
titleLabel.text = @"Creating Account";
titleLabel.font = [UIFont boldFlatFontOfSize:18];
CGSize fittingSize = [titleLabel sizeThatFits:CGSizeMake(200.0f, activityIndicatorView.frame.size.height)];
titleLabel.frame = CGRectMake(activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8,
activityIndicatorView.frame.origin.y,
fittingSize.width,
fittingSize.height);
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(-(activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width)/2,
-(activityIndicatorView.frame.size.height)/2,
activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width,
activityIndicatorView.frame.size.height)];
[titleView addSubview:activityIndicatorView];
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
}
- (void)hideActivityIndicator
{
self.navigationItem.titleView = nil;
}
回答by Vyacheslav
Swift 4
斯威夫特 4
of pasql
pasql的
private func showActivityIndicator() {
let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .white)
activityIndicatorView.frame = CGRect(x: 0, y: 0, width: 14, height: 14)
activityIndicatorView.color = .black
activityIndicatorView.startAnimating()
let titleLabel = UILabel()
titleLabel.text = "...Connecting"
titleLabel.font = UIFont.italicSystemFont(ofSize: 14)
let fittingSize = titleLabel.sizeThatFits(CGSize(width: 200.0, height: activityIndicatorView.frame.size.height))
titleLabel.frame = CGRect(x: activityIndicatorView.frame.origin.x + activityIndicatorView.frame.size.width + 8,
y: activityIndicatorView.frame.origin.y,
width: fittingSize.width,
height: fittingSize.height)
let rect = CGRect(x: (activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width) / 2,
y: (activityIndicatorView.frame.size.height) / 2,
width: activityIndicatorView.frame.size.width + 8 + titleLabel.frame.size.width,
height: activityIndicatorView.frame.size.height)
let titleView = UIView(frame: rect)
titleView.addSubview(activityIndicatorView)
titleView.addSubview(titleLabel)
navigationItem.titleView = titleView
}
private func hideActivityIndicator() {
navigationItem.titleView = nil
}