james
james

Reputation: 646

How to create a Custom UIAlertView in iphone app

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.

Upvotes: 6

Views: 28739

Answers (5)

Rahul
Rahul

Reputation: 333

Create custom view in 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)
  }
}

Upvotes: 0

marika
marika

Reputation: 106

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

Upvotes: 1

Ankit Gupta
Ankit Gupta

Reputation: 958

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

Upvotes: 1

jamil
jamil

Reputation: 2437

Here is link i think it can solve your problem.

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

Upvotes: 2

Saad
Saad

Reputation: 8947

- (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

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)];

                }
            }
        }
    }

Upvotes: 0

Related Questions