如何在警报视图中的文本之间添加图像?
How to add an image in between the texts in a alert view?
我有一个警报视图,我需要在警报视图的文本之间显示一个图标,如下图所示。在我的应用程序中将视频文件异步上传到服务器后出现此警报。任何建议如何去做。
我正在使用 Swift 2.2 xcode 7.3
如果此图片是表情符号或 Unicode 字符,您可以将其直接添加到 UIAlertViewController
中的文本中。不要使用 UIAlertView
,因为它已被弃用。
然而,为了更好的可定制性,我建议创建一个自定义视图并将其添加到您的 VC 中,并在必须显示时使用背景覆盖
如果您想创建自定义提醒,这里有几个开源示例
如果您想使用 UIAlertController
,这个 link 有很好的例子
从 Apple 的 this documentation 开始,您可以使用您想使用的 unicode 标量。
Unicode Scalars
Behind the scenes, Swift’s native String type is built from Unicode
scalar values. A Unicode scalar is a unique 21-bit number for a
character or modifier, such as U+0061 for LATIN SMALL LETTER A ("a"),
or U+1F425 for FRONT-FACING BABY CHICK ("").
此外, 也有帮助。
为什么不创建自定义视图并将其用作警报?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewControllerWithIdentifier("alert")
myAlert.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(myAlert, animated: true, completion: nil)
在那里你可以做任何你想做的事情。
就表情符号而言,您可以从以下位置添加它们:-
Xcode Edit -> Emoji & Symbols
有很多 select 来自 !!!
我通过创建一个自定义视图控制器解决了这个问题,我在中心放置了一个 UIView
,其尺寸与警告框相似,并将其约束设置为 centre horizontally
、centre vertically
, fixed width
和 fixed height
。在里面我放置了一个带有文本的 label
和长 space 在图像必须位于的位置之间。然后我在这个空隙中正确地放置了一个 image view
和图像。
视图的角半径设置为 13px
和背景颜色以匹配警告框样式。
然后按照@Sushree 的回答建议,设置展示样式。但是以编程方式设置它对我不起作用。但是在 StoryBoard
中设置它适用于我检查过的所有 IOS 版本(8.0 到 9.3)。
Select UIViewController
->Attributes inspector
->Presentation
->Over Current Context
我使用 UINavigationController
作为 RootViewController
所以我使用了以下代码
if let wd = appDelegate.window {
var vc = wd.rootViewController
if(vc is UINavigationController){
vc = (vc as! UINavigationController).visibleViewController
vc!.presentViewController(PopupViewController, animated: false, completion: nil)
}
}
引用answer
我有一个警报视图,我需要在警报视图的文本之间显示一个图标,如下图所示。在我的应用程序中将视频文件异步上传到服务器后出现此警报。任何建议如何去做。
我正在使用 Swift 2.2 xcode 7.3
如果此图片是表情符号或 Unicode 字符,您可以将其直接添加到 UIAlertViewController
中的文本中。不要使用 UIAlertView
,因为它已被弃用。
然而,为了更好的可定制性,我建议创建一个自定义视图并将其添加到您的 VC 中,并在必须显示时使用背景覆盖
如果您想创建自定义提醒,这里有几个开源示例
如果您想使用 UIAlertController
,这个 link 有很好的例子
从 Apple 的 this documentation 开始,您可以使用您想使用的 unicode 标量。
Unicode Scalars
Behind the scenes, Swift’s native String type is built from Unicode scalar values. A Unicode scalar is a unique 21-bit number for a character or modifier, such as U+0061 for LATIN SMALL LETTER A ("a"), or U+1F425 for FRONT-FACING BABY CHICK ("").
此外,
为什么不创建自定义视图并将其用作警报?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewControllerWithIdentifier("alert")
myAlert.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(myAlert, animated: true, completion: nil)
在那里你可以做任何你想做的事情。
就表情符号而言,您可以从以下位置添加它们:-
Xcode Edit -> Emoji & Symbols
有很多 select 来自 !!!
我通过创建一个自定义视图控制器解决了这个问题,我在中心放置了一个 UIView
,其尺寸与警告框相似,并将其约束设置为 centre horizontally
、centre vertically
, fixed width
和 fixed height
。在里面我放置了一个带有文本的 label
和长 space 在图像必须位于的位置之间。然后我在这个空隙中正确地放置了一个 image view
和图像。
视图的角半径设置为 13px
和背景颜色以匹配警告框样式。
然后按照@Sushree 的回答建议,设置展示样式。但是以编程方式设置它对我不起作用。但是在 StoryBoard
中设置它适用于我检查过的所有 IOS 版本(8.0 到 9.3)。
Select UIViewController
->Attributes inspector
->Presentation
->Over Current Context
我使用 UINavigationController
作为 RootViewController
所以我使用了以下代码
if let wd = appDelegate.window {
var vc = wd.rootViewController
if(vc is UINavigationController){
vc = (vc as! UINavigationController).visibleViewController
vc!.presentViewController(PopupViewController, animated: false, completion: nil)
}
}
引用answer