[英]UITextField in UIAlertController (border, backgroundColor)
Here is a screenshot of a UIAlertController
.这是
UIAlertController
的屏幕截图。 I was just playing around custom fonts and textfield
properties but I was unable to accomplish the following:我只是在玩自定义字体和
textfield
属性,但我无法完成以下操作:
UITextField
UITextField
清晰背景 As I dived more into the code and iOS runtime headers, I was able to modify border and background color but the above issue still remains as those properties belong to a container UITextView
.当我深入研究代码和 iOS 运行时标头时,我能够修改边框和背景颜色,但上述问题仍然存在,因为这些属性属于容器
UITextView
。 Changing background to clearColor
doesn't help.将背景更改为
clearColor
无济于事。
Has anybody ever played around with this?有没有人玩过这个? Not sure if I would ever take my app into production with such ugly text fields.
不确定我是否会使用如此丑陋的文本字段将我的应用程序投入生产。
EDIT (May 13, 15) The answer below by Rory McKinnel is tested for iOS 8 - 8.3 and works just fine.编辑(15 年 5 月 13 日)下面由 Rory McKinnel 提供的答案已针对 iOS 8 - 8.3 进行了测试,并且运行良好。 The result is below:
结果如下:
Had some fun with this.有一些乐趣。 The following seems to work.
以下似乎有效。 Obviously judging by what was required, it has no future proofing and is a patch away from not working.
显然,从所需的内容来看,它没有未来的证明,并且距离无法正常工作还有一个补丁。
I figured this out by walking the view hierarchy in the debugger, from which I noticed a UIVisualEffectView.我通过在调试器中遍历视图层次结构来解决这个问题,从中我注意到了一个 UIVisualEffectView。 Removing that seems to give you what you want along with setting the containing view to a clear background.
删除它似乎可以为您提供所需的内容,同时将包含视图设置为清晰的背景。 Without removing the visual effect, a clear background shows what is behind the alert view itself for some reason.
在不移除视觉效果的情况下,由于某种原因,清晰的背景会显示警报视图本身背后的内容。
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:@"Its Not Pretty!"
message:@"Some times things get ugly!"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.text = @"Text: No border and clear 8^)";
}];
[self presentViewController:alertController animated:TRUE completion:^{
}];
for (UIView* textfield in alertController.textfields) {
UIView *container = textField.superview;
UIView *effectView = container.superview.subviews[0];
if (effectView && [effectView class] == [UIVisualEffectView class]){
container.backgroundColor = [UIColor clearColor];
[effectView removeFromSuperview];
}
}
here is the important part in swift:这是 swift 中的重要部分:
for textfield: UIView in alertController.textfields {
var container: UIView = textField.superview
var effectView: UIView = container.superview.subviews[0]
container.backgroundColor = UIColor.clearColor()
effectView.removeFromSuperview()
}
You can try this.你可以试试这个。 As you need only clear color to textfield of your alertview.
因为您只需要为警报视图的文本字段清除颜色。 simply add lines of code after your alertview is created.
只需在创建警报视图后添加代码行即可。
UITextField *textField = [alertView textFieldAtIndex:0];
textField.backgroundColor=[UIColor clearColor];
textField.superview.backgroundColor=[UIColor clearColor];
EDIT for alertviewCoontroller you can add编辑alertviewController,您可以添加
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.backgroundColor=[UIColor clearColor];
textField.superview.backgroundColor=[UIColor clearColor];
}];
Thanks, revert if any confusion.谢谢,如果有任何混淆,请回复。
Swift 3 clear version Swift 3 清晰版
alertController.textFields?.forEach {
$0.superview?.backgroundColor = .clear
$0.superview?.superview?.subviews[0].removeFromSuperview()
}
You can change the border and background color like this:您可以像这样更改边框和背景颜色:
let subview = alertController!.view.subviews.first! as UIView
let alertContentView = subview.subviews.first! as UIView
alertContentView.backgroundColor = UIColor.lightGrayColor()
alertContentView.layer.cornerRadius = 10;
alertContentView.layer.borderWidth = 2;
Swift 2.0 version:斯威夫特 2.0 版本:
for textField in alert.textFields! {
if let container = textField.superview, let effectView = container.superview?.subviews.first where effectView is UIVisualEffectView {
container.backgroundColor = UIColor.clearColor()
effectView.removeFromSuperview()
}
}
To address the situation as discussed in @Rory McKinnel and @Matthew where the superview are NULL and address modifying presented view:要解决 @Rory McKinnel 和 @Matthew 中讨论的情况,其中超级视图为 NULL 并且地址修改呈现的视图:
extension UIAlertController {
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.textFields?.forEach {
$0.superview?.backgroundColor = .color
$0.superview?.superview?.subviews[0].removeFromSuperview()
}
}
}
This is very hacky, so examine it well before using (tested on iOS 8.3):这是非常hacky的,所以在使用之前好好检查一下(在iOS 8.3上测试过):
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"This is my placeholder";
textField.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0]; // You can change it to whatever color you want
[textField superview].backgroundColor = textField.backgroundColor;
[[textField superview] superview].backgroundColor = [UIColor whiteColor];
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.