ios 如何在 iphone 应用程序中创建自定义 UIAlertView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10782434/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:31:09  来源:igfitidea点击:

How to create a Custom UIAlertView in iphone app

iphoneiosxcode

提问by james

I have an iphone app and on a button click it should open a custom alert view by showing some text and "X" cross sign on the top right Window as we have in lightbox in any web application.

我有一个 iphone 应用程序,单击按钮它应该打开一个自定义警报视图,方法是在右上角的窗口中显示一些文本和“X”十字符号,就像我们在任何 Web 应用程序中的灯箱中一样。

采纳答案by jamil

回答by Ankit Gupta

If you want to implement Customize alert View then You should this sample Code that have very attractive alert views collection with the help of ViewController. Try this sample Code link https://github.com/eaigner/CODialog

如果您想实现自定义警报视图,那么您应该在 ViewController 的帮助下使用具有非常有吸引力的警报视图集合的示例代码。试试这个示例代码链接 https://github.com/eaigner/CODialog

回答by marika

Here's an example how to create your own class that acts similar to alert view, but you can put any background/button graphics you want:

这是一个如何创建自己的类的示例,该类的行为类似于警报视图,但您可以放置​​所需的任何背景/按钮图形:

http://iosdevtricks.blogspot.com/2013/04/creating-custom-alert-view-for-iphone.html

http://iosdevtricks.blogspot.com/2013/04/creating-custom-alert-view-for-iphone.html

回答by Rahul

Create custom view in ReturnAlert.xib

在 ReturnAlert.xib 中创建自定义视图

enter image description here

在此处输入图片说明

class ReturnView: UIView, UITextFieldDelegate {


}

class ReturnAlert: UIAlertController {
   var returnView: ReturnView?

   public convenience init(id message: String?) {
      self.init(title: nil, message: "message", preferredStyle: .alert)

      guard let view = UINib(nibName: "ReturnAlert", bundle: Bundle(for: ReturnView.self)).instantiate(withOwner: ReturnView.self, options: nil)[0] as? ReturnView else{
        fatalError("Return view not found")
    }

    self.returnView = view
    view.alert = self
    let parent = self.view.subviews[0].subviews[0]

    for sv in parent.subviews {
        sv.removeFromSuperview()
    }

    view.bounds = CGRect(x: parent.bounds.origin.x, y: parent.bounds.origin.y, width: view.frame.size.width, height: view.frame.size.height)

    parent.addSubview(view)

    let xConstraint = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: 0)

    let yConstraint = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: 0)

    parent.addConstraint(xConstraint)
    parent.addConstraint(yConstraint)
  }
}

回答by Saad

- (void)willPresentAlertView:(UIAlertView *)alertView; 
- (void)didPresentAlertView:(UIAlertView *)alertView; 

in any of the above message, check the subviews and their class and change the values as you desire. see this sample code for UIActionSheet. search for classes of all components using ns log and customize your desire class. This is uiactionsheet code

在上述任何消息中,检查子视图及其类并根据需要更改值。请参阅 UIActionSheet 的示例代码。使用 ns log 搜索所有组件的类并自定义您想要的类。这是uiactionsheet代码

for (UIView* view in [actionSheet subviews])
    {
        NSLog(@"%@",[view class]);
        if ([[[view class] description] isEqualToString:@"UIAlertButton"] && [view respondsToSelector:@selector(setAlpha:)])
        {
            [view setAlpha:2.0];
            [view setOpaque:YES];
            if ([view respondsToSelector:@selector(title)])
            {
                NSString* title = [view performSelector:@selector(title)];
                if ([title isEqualToString:@"Cancel"] && [view respondsToSelector:@selector(setBackgroundImage:forState:)] && [view respondsToSelector:@selector(setFrame:)] && [view respondsToSelector:@selector(setFrame:)] && [view respondsToSelector:@selector(setTitleColor:forState:)])
                {

                    [view setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    [view setBackgroundImage:[UIImage imageNamed:@"btn-cancel.png"] forState:UIControlStateNormal];
                    [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y+10, view.frame.size.width,view.frame.size.height)];

                }
            }
        }
    }