Custom view which looks like UIAlertView
I created my own custom view to look like iOS UIAlertView 7. With that technique you can create a custom alert for both iOS 6 and iOS 7.
For that, I created a UIView in my xib file of my UIViewController :
I added some @property for this view :
// Custom iOS 7 Alert View
@property (nonatomic, weak) IBOutlet UIView *supportViewPopup; // My UIView
@property (nonatomic, weak) IBOutlet UIView *supportViewPopupBackground; // The grey view
@property (nonatomic, weak) IBOutlet UIView *supportViewPopupAction; // The white view with outlets
// Property for customize the UI of this alert (you can add other labels, buttons, tableview, etc.
@property (nonatomic, weak) IBOutlet UIButton *buttonOK;
@property (nonatomic, weak) IBOutlet UIButton *buttonCancel;
@property (nonatomic, weak) IBOutlet UILabel *labelDescription;
On my viewDidLoad :
- (void)viewDidLoad
{
[super viewDidLoad];
// Support View
self.supportViewPopupAction.layer.cornerRadius = 5.0f;
self.supportViewPopupAction.layer.masksToBounds = YES;
// Add Support View
[self.view addSubview:self.supportViewPopup];
// Center Support view
self.supportViewPopup.center = self.view.center;
// Alpha
self.supportViewPopup.alpha = 0.0f;
self.supportViewPopupBackground.alpha = 0.0f;
self.supportViewPopupAction.alpha = 0.0f;
}
Action to display Popup :
- (IBAction)displayPopup
{
// Support View
self.supportViewPopup.alpha = 1.0f;
self.supportViewPopupBackground.alpha = 0.5f;
// Animation
[UIView animateWithDuration:0.5f
animations:^{
self.supportViewPopupAction.alpha = 1.0f;
}];
}
Action to dismiss Popup :
- (IBAction)dismissModal
{
// Animation
[UIView animateWithDuration:0.5f
animations:^{
self.supportViewPopup.alpha = 0.0f;
self.supportViewPopupBackground.alpha = 0.0f;
self.supportViewPopupAction.alpha = 0.0f;
}];
}
So, with that you can configure your supportViewPopupAction
like you want with buttons, table view, labels, collection view, etc...
I spent time to write this example of alert view. I hope this will help you !
How To Create Custom view That looks like Alert View?
Use this.
self.blureEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:self.blureEffect];
self.visualEffectView.frame = self.view.bounds;
self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[[[UIApplication sharedApplication] keyWindow] addSubview:self.visualEffectView];
adding a custom view to a alert view
for all who having this problem,
i have followed this link..and for the star marks, i used this..
all you have to do is add below files to the project
- CustomIOS7AlertView.h
- CustomIOS7AlertView.m
- JSFavStarControl.h
- JSFavStarControl.m
and put below code where you want to pop up the alert view
// Here we need to pass a full frame
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
// Add some custom content to the alert view
[alertView setContainerView:[self createDemoView]];
// Modify the parameters
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close1", @"Close2", @"Close3", nil]];
[alertView setDelegate:self];
// You may use a Block, rather than a delegate.
[alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
[alertView close];
}];
[alertView setUseMotionEffects:true];
// And launch the dialog
[alertView show];
and inside the method of createDemoView,you have to implement your customised view.in my case it is like this
UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
//alertView.tag=2;
UILabel *rateLbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 45)];
rateLbl.backgroundColor=[UIColor clearColor];
rateLbl.font=[UIFont boldSystemFontOfSize:15];
rateLbl.text=@"Rate";
UILabel *cmntLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 290, 45)];
cmntLable.backgroundColor=[UIColor clearColor];
cmntLable.font=[UIFont boldSystemFontOfSize:15];
cmntLable.text=@"Add Comment";
UIImage *dot, *star;
dot = [UIImage imageNamed:@"dot.png"];
star = [UIImage imageNamed:@"star.png"];
JSFavStarControl *rating = [[JSFavStarControl alloc] initWithLocation:CGPointMake(150, 20) dotImage:dot starImage:star];
[rating addTarget:self action:@selector(updateRating:) forControlEvents:UIControlEventValueChanged];
UILabel *lblAlertTItle=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 290, 45)];
lblAlertTItle.backgroundColor=[UIColor clearColor];
lblAlertTItle.textAlignment=UITextAlignmentCenter;
lblAlertTItle.font=[UIFont boldSystemFontOfSize:18];
lblAlertTItle.text=@"Choose your sharing option";
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(150, 57, 100, 25)];
text.backgroundColor=[UIColor whiteColor];
//[demoView addSubview:lblAlertTItle];
[demoView addSubview:text];
[demoView addSubview:rating];
[demoView addSubview:rateLbl];
[demoView addSubview:cmntLable];
return demoView;
so my output is like this.
use it and have fun :)
thank you for everyone who helped me.
How to make a UIView that looks exactly like UIAlertView?
What you're looking for is a Blur Effect.
If you're target is iOS 8 and higher, you can use this to create blur views
UIBlurEffect* blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
effectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
effectView.frame = self.bounds;
[self addSubview:effectView];
Apple documentation
Note that you can use UIBlurEffectStyleExtraLight, UIBlurEffectStyleLight, UIBlurEffectStyleDark.
If you use a lower version of iOS, you've to use a custom class like FXBlurView.
Custom Alert (UIAlertView) with swift
Code tested in Swift 5 and Xcode 10
How to make your own custom Alert
I was wanting to do something similar. First of all, UIAlertView
is deprecated in favor of UIAlertController
. See this answer for the standard way to display an alert:
- How would I create a UIAlertView in Swift?
And both UIAlertView
and UIAlertController
do not really allow much customization. One option is to use some third party code. However, I discovered that it isn't that difficult to create your own Alert by displaying another view controller modaly.
The example here is just a proof-of-concept. You can design your alert any way you want.
Storyboard
You should have two View Controllers. Your second view controller will be your alert. Set the class name to AlertViewContoller
and the Storyboard ID to alert
. (Both of these are names that we defined ourselves in the code below, nothing special about them. You can add the code first if you want. It might actually be easier if you add the code first.)
Set the background color for the root view (in your Alert View Controller) to clear (or translucent black is nice for an alert). Add another UIView
and center it with constraints. Use that as your alert background and put whatever you want inside. For my example, I added a UIButton
.
Code
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "alert")
myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(myAlert, animated: true, completion: nil)
}
}
AlertViewController.swift
import UIKit
class AlertViewController: UIViewController {
@IBAction func dismissButtonTapped(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
}
Don't forget to hook up the outlets.
You can add an onTouchUp
event listener to the background view to dismiss the popup when the user clicks outside of it.
That's it. You should be able to make any sort of alert that you can imagine now. No need for third party code.
Here is another custom alert I made. Still ugly, but it shows more things you can do.
Other options
Sometimes there is no need to reinvent the wheel, though. I'm impressed with the third party project SDCAlertView (MIT license). It is written in Swift but you can use it with Objective-C projects as well. It offers a wide range of customability.
Swift Custom UIAlertView
Something like the following should allow it. Note there quite a few improvements that could be made. For example you could use a generic for the object being deleted instead of AnyObject. You also don't necessarily need to pass it in if you pass the closure inline anyway so you could probably just remove it.
You could also make your buttons more reusable rather than hard-coding to cancel and remove but now we're getting off topic :)
class ConfirmViewController : UIViewController {
var onCancel : (() -> Void)?
var onConfirm : ((AnyObject?) -> Void)?
var objectToDelete : AnyObject?
func cancelButtonPressed() {
// defered to ensure it is performed no matter what code path is taken
defer {
dismissViewControllerAnimated(false, completion: nil)
}
let onCancel = self.onCancel
// deliberately set to nil just in case there is a self reference
self.onCancel = nil
guard let block = onCancel else { return }
block()
}
func confirmationButtonPresssed() {
// defered to ensure it is performed no matter what code path is taken
defer {
dismissViewControllerAnimated(false, completion: nil)
}
let onConfirm = self.onConfirm
// deliberately set to nil just in case there is a self reference
self.onConfirm = nil
guard let block = onConfirm else { return }
block(self.objectToDelete)
}
}
let confirm = ConfirmViewController()
confirm.objectToDelete = NSObject()
confirm.onCancel = {
// perform some action here
}
confirm.onConfirm = { objectToDelete in
// delete your object here
}
Should i be using UIAlertView or a custom view?
Take a look at the class-documentation:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
UIAlertView supports a text-field by default (take a look at textFieldAtIndex:).
So of course you can use it to get input from your users
Related Topics
Objective-C Check If Subviews of Rotated Uiviews Intersect
Intercept Request with Wkwebview
How to Pass Data to Another Controller on Dismiss Viewcontroller
Add a Scrollview to Existing View
Uiwindow with Wrong Size When Using Landscape Orientation
Core Location Not Working in iOS 8
Uitapgesturerecognizer Programmatically Trigger a Tap in My View
How to Get Change in Network Connection Notification from iOS Reachability Class
Passing Data with Segue Through Navigationcontroller
Ios: Rounded Rectangle with Border Bleeds Color
How to Have One Static Cell in a Dynamic Tableview
iPad iOS7 - Uiimagepickercontroller in Uipopovercontroller Has Wrong Preview Image
Ios/Swift - Hide/Show Uitabbarcontroller When Scrolling Down/Up
How to Change the Development Language in Xcode Before Internationalizing My App
Apple MACh-O Linker & Ditto Error - Xcode 8