如何在 UIToolbar 中的 UIProgressView 上方添加标签?

发布于 2024-09-12 13:43:26 字数 416 浏览 4 评论 0原文

我有一个 UIToolbar,其中已成功放置 UIProgressView。然而,我看到一些应用程序在 UIProgressView 上方包含一个小标签,它告诉用户程序正在做什么以及正在取得进展 - 例如下载文件。然而,这似乎无法在 UI Builder 中完成。关于在工具栏中添加 UIProgressView 标签的最佳方法有什么想法吗?这是我感兴趣的:

+------------------------------------------------+
|         Uploading File                         |
| ================--------------------  [CANCEL] |
+------------------------------------------------+

I have a UIToolbar in which I have placed a UIProgressView successfully. However, I have seen some apps contain a small label above the UIProgressView which tells the user what the program is doing where progress is being made -- e.g. to download a file. However it seems that this cannot be done in UI Builder. Any ideas on the best way to add the label ablve the UIProgressView in the toolbar? Here is what I am interested in:

+------------------------------------------------+
|         Uploading File                         |
| ================--------------------  [CANCEL] |
+------------------------------------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-09-19 13:43:26

创建一个自定义 UIView,其中包含 UILabelUIProgressView 作为子视图。然后将自定义 UIView 插入工具栏中。

Make a custom UIView that contains a UILabel and UIProgressView as subviews. You then insert the custom UIView into the toolbar.

冰雪梦之恋 2024-09-19 13:43:26

实际上,您还可以将文本作为子视图直接添加到 UIProgressView 中,例如:

UIProgressView *videoProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height/2, self.view.frame.size.width - 80, 40)];

UILabel *processing = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, videoProgressView.frame.size.width, 25)];
processing.text = @"Processing Video...";
processing.textAlignment = NSTextAlignmentCenter;

[videoProgressView addSubview:processing];

[self.view addSubview:videoProgressView];

只需确保 UIProgressViewclipsToBounds 属性设置为 NO。

You can actually also add text directly to a UIProgressView as a subview, ex:

UIProgressView *videoProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height/2, self.view.frame.size.width - 80, 40)];

UILabel *processing = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, videoProgressView.frame.size.width, 25)];
processing.text = @"Processing Video...";
processing.textAlignment = NSTextAlignmentCenter;

[videoProgressView addSubview:processing];

[self.view addSubview:videoProgressView];

Just make sure UIProgressView's clipsToBounds property is set to NO.

药祭#氼 2024-09-19 13:43:26

以下是我的项目中使用的 Lyndsey Scott 的答案 的 Swift 5 实现:

let view = UIApplication.topViewController()!.view!

let progressView = UIProgressView(progressViewStyle: .default)
progressView.center = view.center
progressView.trackTintColor = .gray
progressView.frame = CGRect(x: 40, y: view.frame.size.height / 2, width: view.frame.size.width - 80, height: 40)
      
let progressViewLabel = UILabel(frame: CGRect(x: 0, y: -50, width: progressView.frame.size.width, height: 25))
progressViewLabel.text = "Migrating database:"
progressViewLabel.textAlignment = .center
      
progressView.addSubview(progressViewLabel)
      
view.addSubview(progressView)

Here's a Swift 5 implementation of Lyndsey Scott's answer used in my project:

let view = UIApplication.topViewController()!.view!

let progressView = UIProgressView(progressViewStyle: .default)
progressView.center = view.center
progressView.trackTintColor = .gray
progressView.frame = CGRect(x: 40, y: view.frame.size.height / 2, width: view.frame.size.width - 80, height: 40)
      
let progressViewLabel = UILabel(frame: CGRect(x: 0, y: -50, width: progressView.frame.size.width, height: 25))
progressViewLabel.text = "Migrating database:"
progressViewLabel.textAlignment = .center
      
progressView.addSubview(progressViewLabel)
      
view.addSubview(progressView)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文