Custom Alertview with delegates without storyboard - ios

I have design custom alerview for my project.
I create a seperate viewcontroller(.h and .m) files with #protocol for the delegates to get alertview button click response. I have create an object for the view controller(custom alertview which i create with custom delegate). and I subview the custom alertview in each of other classes. Its working as normal UIAlertview.
But the issue occurs on when I show the alertview and then press the home button and return to my app its getting freeze in ios7 and in ios6 getting crash.
What should I do to fix this issue. It(alert view) should work as normal when i return to my app. Please give your ideas to fix this.
I cant find how the apple default UIAlertview work when in the same(dipaly alertview and enter into background and rentun into app the displayed alertview works fine).
I m not use UIAlertvie
alert_cupboard=[[UIAlertView alloc]initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Yes",#"No", nil];
alert_cupboard.tag=0;
[alert_cupboard show];
For that, I have design in My code for create Custom Alertview
CustomAlertView.h
#protocol AlertViewprotocal <NSObject>
#required
- (void)AlertSuccess:(id)result;
- (void)AlertFailure:(id)result;
#end
#interface CustomAlertView : UIViewController
{
UIView *view_alert;
UIImageView *img_alert_bg;
UIView *view_alert_popup;
UILabel *lbl_alert_title;
UILabel *lbl_alert_message;
UILabel *lbl_sep_horizontal;
UILabel *lbl_sep_vertical;
UIButton *btn_alert_OK;
UIButton *btn_alert_yes;
UIButton *btn_alert_no;
}
#property(nonatomic,weak)id<AlertViewprotocal> CustomAlertdelegate;
+(CustomAlertView *)singleton;
-(void)showAlertView:(UIViewController *)rootViewController:(NSInteger)option :(NSString *)message :(NSInteger)tag;
-(IBAction)action_alert_yes:(id)sender;
-(IBAction)action_alert_hide:(id)sender;
#end
CustomAlertView.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
float height=[UIScreen mainScreen].bounds.size.height;
float width=[UIScreen mainScreen].bounds.size.width;
view_alert=[[UIView alloc]initWithFrame:CGRectMake(0, 0,width , height)];
img_alert_bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,width , height)];
[img_alert_bg setImage:[UIImage imageNamed:#"pop_up_bg.png"]];
[view_alert addSubview:img_alert_bg];
view_alert_popup=[[UIView alloc]initWithFrame:CGRectMake(IS_IPAD ? (234): 10, (height-156)/2, IS_IPAD ? (300):(width-(10*2)) , 156)];
[view_alert_popup setBackgroundColor:[UIColor whiteColor]];
view_alert_popup.layer.cornerRadius= 10.5; //IS_IPAD ?8.2:
lbl_alert_title=[[UILabel alloc]initWithFrame:CGRectMake(20, 5, 260, 35.0)];
[lbl_alert_title setTextColor:[UIColor blackColor]];
[lbl_alert_title setBackgroundColor:[UIColor clearColor]];
[lbl_alert_title setText:#"Title"];
lbl_alert_title.textAlignment = UITextAlignmentCenter;
lbl_alert_title.font = [UIFont fontWithName:#"Helvetica" size: 18.0]; //IS_IPAD ? 34.0 :
[view_alert_popup addSubview:lbl_alert_title];
lbl_alert_message=[[UILabel alloc]initWithFrame:CGRectMake(20, lbl_alert_title.frame.size.height, view_alert_popup.frame.size.width-(20*2), 70)];
[lbl_alert_message setTextColor:[UIColor blackColor]];
[lbl_alert_message setBackgroundColor:[UIColor clearColor]];
lbl_alert_message.font = [UIFont fontWithName:#"Helvetica" size: 16.0]; //IS_IPAD ? 34.0 :
lbl_alert_message.textAlignment = UITextAlignmentCenter;
lbl_alert_message.numberOfLines=3;
lbl_alert_message.lineBreakMode=UILineBreakModeWordWrap;
[view_alert_popup addSubview:lbl_alert_message];
lbl_sep_horizontal=[[UILabel alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height), view_alert_popup.frame.size.width, 1.0)];
[lbl_sep_horizontal setBackgroundColor:[UIColor lightGrayColor]];
[view_alert_popup addSubview:lbl_sep_horizontal];
lbl_sep_vertical=[[UILabel alloc]initWithFrame:CGRectMake(150, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height), 1.0, 50.0)];
[lbl_sep_vertical setBackgroundColor:[UIColor lightGrayColor]];
[view_alert_popup addSubview:lbl_sep_vertical];
btn_alert_yes=[[UIButton alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 150, 30.0)];
[btn_alert_yes setBackgroundColor:[UIColor clearColor]];
[btn_alert_yes setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_yes.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size: 16.0]];
[btn_alert_yes setTitle:#"Yes" forState:UIControlStateNormal];
//[btn_alert_yes.titleLabel setText:#"Yes"];
[btn_alert_yes addTarget:self action:#selector(action_alert_yes:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_yes];
btn_alert_no=[[UIButton alloc]initWithFrame:CGRectMake(150, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 150, 30.0)];
[btn_alert_no setBackgroundColor:[UIColor clearColor]];
[btn_alert_no setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_no.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size: 16.0]];
[btn_alert_no setTitle:#"No" forState:UIControlStateNormal];
//[btn_alert_no.titleLabel setText:#"No"];
[btn_alert_no addTarget:self action:#selector(action_alert_hide:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_no];
btn_alert_OK=[[UIButton alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 300, 30.0)];
[btn_alert_OK setBackgroundColor:[UIColor clearColor]];
[btn_alert_OK setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_OK.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size: 16.0]];
[btn_alert_OK setTitle:#"OK" forState:UIControlStateNormal];
//[btn_alert_OK.titleLabel setText:#"OK"];
[btn_alert_OK addTarget:self action:#selector(action_alert_hide:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_OK];
[view_alert addSubview:view_alert_popup];
[self.view addSubview:view_alert];
}
return self;
}
+(CustomAlertView *)singleton
{
if (shared)
{
shared=nil;
}
shared = [[CustomAlertView alloc] init];
return shared;
}
-(void)showAlertView:(UIViewController *)rootViewController:(NSInteger)option :(NSString *)message :(NSInteger)tag
{
lbl_alert_message.text=message;
view_alert.tag=tag;
if (option==1)
{
[btn_alert_OK setHidden:NO];
btn_alert_OK.tag=tag;
[btn_alert_yes setHidden:YES];
[btn_alert_no setHidden:YES];
[lbl_sep_horizontal setHidden:NO];
[lbl_sep_vertical setHidden:YES];
}
else
{
[btn_alert_OK setHidden:YES];
[btn_alert_yes setHidden:NO];
[btn_alert_no setHidden:NO];
[lbl_sep_horizontal setHidden:NO];
[lbl_sep_vertical setHidden:NO];
btn_alert_yes.tag=tag;
}
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView animateWithDuration:0.4/1.5 animations:^
{
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.4/2 animations:^
{
[rootViewController.view addSubview:self.view];
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.4/2 animations:^{
view_alert_popup.transform = CGAffineTransformIdentity;
}];
}];
}];
}
-(IBAction)action_alert_yes:(id)sender
{
[self.view removeFromSuperview];
[self.CustomAlertdelegate AlertSuccess:view_alert];
}
-(IBAction)action_alert_hide:(id)sender
{
[self.view removeFromSuperview];
[self.CustomAlertdelegate AlertFailure:view_alert];
}
I have use the custome alertview in other my view controller
HomeView.h
#interface HomeScreen :UIViewController <AlertViewprotocal> //CustomAlertView
{
CustomAlertView *alert;
}
HomeView.m
-(void)viewWillAppear:(BOOL)animated
{
alert=[CustomAlertView singleton];
alert.CustomAlertdelegate=self;
}
- (void)viewWillDisappear:(BOOL)animated
{
alert.CustomAlertdelegate=nil;
}
-(IBAction)action_addRemove:(id)sender
{
UIButton *btn=sender;
self.add_row=btn.tag;
[alert showAlertView:self :2 :#"message" :0];
}
- (void)AlertSuccess:(id)result
{
UIView *vw=result;
NSLog(#"Success tag:%d",vw.tag);
switch (vw.tag)
{
case 0:
{
[self remove];
}
break;
case 1:
{
[self add];
}
break;
default:
break;
}
}
- (void)AlertFailure:(id)result
{
UIView *vw=result;
NSLog(#"Failure tag:%d",vw.tag);
}
I got
-[__NSArrayM action_alert_yes:]: unrecognized selector sent to instance 0xc135350
Stack trace: NSInvalidArgumentException

I think you have not put "nil" parameter at the end of your custom alert view.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error" message:#"message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"otherbutton", nil];
nil is last parameter.

Related

UIButton isn't responding to my touches on collectionView cells?

I'm creating collectionView and inside that in every cell I'm creating few buttons horizontally but those buttons aren't responding to my target selector.
Here is my code.
ImplementationView.h
-(IBAction)tagClicked:(UIButton*)sender;
In ImplementationView.m
-(void)configureScrapCell:(UICollectionViewCell*) cell forScrapDict:(ScrapDictionary *)dict forIndexPath:(NSIndexPath*){
UIView *tagView = (UIView*)[cell viewWithTag:17];
UIView *layerTagView = (UIView*)[cell viewWithTag:18];
CGFloat x = 0.0;
if (layerTagView) {
[layerTagView removeFromSuperview];
}
layerTagView = [[UIView alloc] init];
layerTagView.tag = 18;
[tagView addSubview:layerTagView];
if ([dict.SDtags count]>0) {
int tagIndex = 1;
for (NSString *tag in dict.SDtags) {
CGSize width = [tag sizeWithFont:[UIFont systemFontOfSize:16]];
UIButton *scrapTag = [[UIButton alloc]initWithFrame:CGRectMake(x, 0,100, 30)];
[scrapTag setBackgroundColor:[UIColor greenColor]];
[scrapTag.titleLabel setFont:[UIFont systemFontOfSize:14]];
// [scrapTag setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
if (brightness > 150) {
[scrapTag setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}else {
[scrapTag setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
[scrapTag setTitle:[NSString stringWithFormat:#" #%#",tag] forState:UIControlStateNormal];
// [scrapTag sizeToFit];
[scrapTag setUserInteractionEnabled:YES];
tagIndex++;
NSString *tagString = #"";
tagString = [tagString stringByAppendingString:tag];
if ([tagString length]>32 || tagIndex>4) {
break;
}
[scrapTag addTarget:self action:#selector(tagClicked:) forControlEvents:UIControlEventTouchUpInside];
[layerTagView addSubview:scrapTag];
NSLog(#"%f",scrapTag.frame.size.width);
// scrapTag.frame = CGRectMake(x, scrapTag.frame.origin.y, tagView.frame.size.width, 30);
x += scrapTag.frame.size.width +2;
}
}
[tagView setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:0.5]];
[layerTagView setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:0.5]];
Now in different class where I'm defining the action of these button Clicked. ImplementationView class is acting as delegate for NewClass.
NewClass.h
#import ImplementationView.h
#interface NewClass : ImplementationView
NewClass.m
-(IBAction)tagClicked:(UIButton *)sender {
[[[UIAlertView alloc]initWithTitle:#"LOL" message:[NSString stringWithFormat:#"%#",sender.titleLabel.text] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil]show ];
NSLog(#"testing tag clicked");
}
Let me know if you need any other help to make question clear. Any help will be really appreciated. Thanks in advance.
Your buttons are not on the layerTagView bounds.
You need to set layerTagView frame/size. In your code the size of layerTagView is (0,0).
For check my suggestions you can set masksToBounds to YES or change the background color for layerTagView to blue or any other color.
tagView.layer.masksToBounds = YES;
layerTagView.layer.masksToBounds = YES;

Show a UIViewController as Alert View in iOS

I'm trying to create a custom Alert View similar to this
I want to use a View Controller as the alert because the 'X' button on the top left corner must be there, and neither a regular UIAlertController or any pod I found can help me with that.
How can I achieve this?
Thank you.
For showing viewController as UIAlertView try this
SecondViewController *secondViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"SecondViewController"];
secondViewController.view.frame = self.view.frame;
[self.view addSubview:secondViewController.view];
secondViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
secondViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
}];
try this..
#interface alertViewController ()
{
UILabel *labelTitle;
UIView *viewToShow,*viewAlert;
}
#end
#implementation alertViewController
- (void)viewDidLoad
{
[super viewDidLoad];
viewToShow = [[UIView alloc]initWithFrame:CGRectMake(0,0, 165*2, 400)];
[viewToShow setBackgroundColor:[UIColor whiteColor]];
[viewToShow setOpaque:NO];
[self.navigationController.view addSubview:viewToShow];
labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 165*2, 30)];
[[labelTitle layer] setCornerRadius:14];
labelTitle.text=#"TITLE";
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[viewToShow addSubview:labelTitle];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"X" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"helvetica-neue" size:20]];
button.frame = CGRectMake(5,5,30, 40.0);
[viewToShow addSubview:button];
UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 60,viewToShow.frame.size.width-20, 250)];
[textView setBackgroundColor:[UIColor lightGrayColor]];
[viewToShow addSubview:textView];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Continue" forState:UIControlStateNormal];
button1.frame = CGRectMake((viewToShow.frame.size.width/2)-40,viewToShow.frame.size.height-40,80,40);
[viewToShow addSubview:button1];
[[viewToShow layer] setCornerRadius:14];
CGPoint centerForCustomExportView;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
centerForCustomExportView = CGPointMake(([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.height:[[UIScreen mainScreen]bounds].size.width)/2, (([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2));
}
else
{
centerForCustomExportView = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, ([[UIScreen mainScreen]bounds].size.height-self.navigationController.navigationBar.frame.size.height)/2);
}
viewToShow.center = centerForCustomExportView;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification object:nil];
}
try this.....
- (void)show
{
NSLog(#"show");
isShown = YES;
self.transform = CGAffineTransformMakeScale(0.1, 0.1);
self.alpha = 0;
[UIView beginAnimations:#"showAlert" context:nil];
[UIView setAnimationDelegate:self];
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
self.alpha = 1;
[UIView commitAnimations];
}

InputAccessoryView Border?

I'm creating a custom keyboard accessory view. I am currently about 95% done with it. The only thing I have left is to remove the 1px (or 2px?) line above the native iOS keyboard. Is there a way to add a border to the inputaccessoryview so I can use the same color as the keyboard gradient to eliminate the black bar?
Picture Example
If the boarder isn't possible what is the next most logical thing to do with my source code?
signInViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#interface signInViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate> {
AVAudioPlayer *sound;
UIButton *myButton;
UITextField *textFieldOne;
UIButton *gmailButton;
UIButton *meButton;
UIButton *yahooButton;
UIButton *outlookButton;
UIView *inputAccView;
UIButton *aolButton;
UIButton *milButton;
}
- (IBAction)playSwoosh:(id)sender;
- (IBAction)dismiss:(id)sender;
- (IBAction)toggleUIButtonImage:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *textFieldOne;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTwo;
#property (nonatomic, retain) UIButton *gmailButton;
#property (nonatomic, retain) UIButton *meButton;
#property (nonatomic, retain) UIButton *yahooButton;
#property (nonatomic, retain) UIButton *outlookButton;
#property (nonatomic, retain) UIButton *aolButton;
#property (nonatomic, retain) UIButton *milButton;
#property (retain, nonatomic) UIButton *myButton;
#property (nonatomic, retain) UIView *inputAccView;
#end
and signInViewController.m
#import "signInViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface signInViewController ()
#end
#implementation signInViewController
#synthesize myButton = _myButton;
#synthesize textFieldOne = _textFieldOne;
#synthesize textFieldTwo = _textFieldTwo;
#synthesize inputAccView;
#synthesize gmailButton;
#synthesize yahooButton;
#synthesize meButton;
#synthesize outlookButton;
#synthesize aolButton;
#synthesize milButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)gmailButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#gmail.com"];
}
- (void)meButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#me.com"];
}
- (void)outlookButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#outlook.com"];
}
- (void)yahooButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#yahoo.com"];
}
- (void)aolButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#aol.com"];
}
- (void)milButtonTapped
{
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:#"#mail.mil"];
}
- (void)createInputAccessoryView {
inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
[inputAccView setBackgroundColor:[UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000]];
[inputAccView setAlpha: 0.8];
// GMAIL BUTTON ADDED
gmailButton = [UIButton buttonWithType: UIButtonTypeCustom];
[gmailButton setFrame:CGRectMake(9.5, 4.5, 60.0, 30.0)];
[gmailButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:gmailButton];
gmailButton.layer.cornerRadius = 5;
gmailButton.layer.masksToBounds = NO;
gmailButton.layer.shadowColor = [UIColor blackColor].CGColor;
gmailButton.layer.shadowOpacity = 0.8;
gmailButton.layer.shadowRadius = 2;
gmailButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[gmailButton setTitle:#"#Gmail" forState:UIControlStateNormal];
[gmailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[gmailButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
gmailButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue Bold" size:12];
[gmailButton addTarget:self action:#selector(gmailButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// ME BUTTON ADDED
meButton = [UIButton buttonWithType: UIButtonTypeCustom];
[meButton setFrame:CGRectMake(73.5, 4.5, 40.0, 30.0)];
[meButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:meButton];
meButton.layer.cornerRadius = 5;
meButton.layer.masksToBounds = NO;
meButton.layer.shadowColor = [UIColor blackColor].CGColor;
meButton.layer.shadowOpacity = 0.8;
meButton.layer.shadowRadius = 2;
meButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[meButton setTitle:#"#Me" forState:UIControlStateNormal];
[meButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[meButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
meButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue Bold" size:12];
[meButton addTarget:self action:#selector(meButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// YAHOO BUTTON ADDED
yahooButton = [UIButton buttonWithType: UIButtonTypeCustom];
[yahooButton setFrame:CGRectMake(117.5, 4.5, 65.0, 30.0)];
[yahooButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:yahooButton];
yahooButton.layer.cornerRadius = 5;
yahooButton.layer.masksToBounds = NO;
yahooButton.layer.shadowColor = [UIColor blackColor].CGColor;
yahooButton.layer.shadowOpacity = 0.8;
yahooButton.layer.shadowRadius = 2;
yahooButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[yahooButton setTitle:#"#Yahoo" forState:UIControlStateNormal];
[yahooButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[yahooButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
yahooButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue Bold" size:12];
[yahooButton addTarget:self action:#selector(yahooButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// OUTLOOK BUTTON ADDED
outlookButton = [UIButton buttonWithType: UIButtonTypeCustom];
[outlookButton setFrame:CGRectMake(186.5, 4.5, 77.0, 30.0)];
[outlookButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:outlookButton];
outlookButton.layer.cornerRadius = 5;
outlookButton.layer.masksToBounds = NO;
outlookButton.layer.shadowColor = [UIColor blackColor].CGColor;
outlookButton.layer.shadowOpacity = 0.8;
outlookButton.layer.shadowRadius = 2;
outlookButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[outlookButton setTitle:#"#Outlook" forState:UIControlStateNormal];
[outlookButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[outlookButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
outlookButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue Bold" size:12];
[outlookButton addTarget:self action:#selector(outlookButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// AOL BUTTON ADDED
aolButton = [UIButton buttonWithType: UIButtonTypeCustom];
[aolButton setFrame:CGRectMake(267.5, 4.5, 41.0, 30.0)];
[aolButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:aolButton];
aolButton.layer.cornerRadius = 5;
aolButton.layer.masksToBounds = NO;
aolButton.layer.shadowColor = [UIColor blackColor].CGColor;
aolButton.layer.shadowOpacity = 0.8;
aolButton.layer.shadowRadius = 2;
aolButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[aolButton setTitle:#"#Aol" forState:UIControlStateNormal];
[aolButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aolButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
aolButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue Bold" size:12];
[aolButton addTarget:self action:#selector(aolButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)backgroundTouched:(id)sender {
[_textFieldOne resignFirstResponder];
[_textFieldTwo resignFirstResponder];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.textFieldOne) {
[self createInputAccessoryView];
[textField setInputAccessoryView:inputAccView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
} else if (textField == self.textFieldTwo) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.textFieldOne) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
} else if (textField == self.textFieldTwo) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}
Well, after playing around with it, I've decided not to add a boarder because all it did was include an inside boarder. I simply just drew an object to cover it up. But I did find a great resource for those still wondering how to do it! This is another method that would be great for implementation. For you new developers that just want a quick fix, someone did offer for me to simply create another button that was the length of the screen and 3 px high to cover it up, but we all know that s the wrong answer. Just a tip for any newbies
Extend iOS Keyboard Gradient
The KeyboardAccessoryView has a special property hideBorder according to the docs at https://github.com/ardaogulcan/react-native-keyboard-accessory
I suppose InputAccessoryView has the same property.

How can I load a ViewController or View into another ViewController with transparent background?

In my app I want to have a custom popup to appear when called from a UIButton. Currently I'm using TSAlertView, but I want to implement more UI items and have a more custom build. So what I would like to do is load a viewcontroller or view within another viewcontroller and be able to pass information along between them both. See the example below:
From what I've read, supposedly it is not good practice to load another viewcontroller within another. In this case I would assume a uiview would be the recommended way. Is this correct?
I would like the viewcontroller behind the popup to have dimmed look, maybe a dark color with the opacity set at 70%. How can I achieve this?
Essentially I'm trying to build something similar to an AlertView with the appearance of another view.
If you intend to completely block the background view, you could set viewControllerB to Form Sheet.
When you are ready to show viewControllerB, use:
[viewControllerA presentViewController:viewControllerB animated:TRUE completion:nil]
You will need a way to dismiss viewControllerB; a button, network action, etc.
An example with Storyboards:
- (void)someMethod {
UIStoryboard *storyboard = self.storyboard;
ViewControllerB *viewCotrollerB = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
[self presentModalViewController:viewControllerB animated:YES];
}
Hi SReca,I was also having same requirement(like shown in above image) to create view like UIalertview which you are saying.
Here you will have to create a new UIView class instead of UIViewController, with no need of .xib.
In the new UIView's class in init method you will require to create your userinterface programmatically. And keep its height width as much as you want.
And for loading this new UIView you will have to just create object of that uiview and and call initwithframe it will present that view programmatically like uialerview.
Here is code for referece.
ModalPreviewViewController_iPhone.h
#interface ModalPreviewViewController_iPhone : UIView
#end
ModalPreviewViewController_iPhone.m
just modify this example code as per your need in initWithFrame
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30,30,260,420)];
popUpBgView = [[UIView alloc] init];
popUpBgView.frame =CGRectMake(0,0,260,420);
[popUpBgView setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
[popUpView addSubview:popUpBgView];
imageGalleryView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 260, 360)];
[imageGalleryView setBackgroundColor:[UIColor redColor]];
CGRect btnTempFrame=CGRectMake(5, 385, 90, 30);
btnTemp=[[UIButton alloc]initWithFrame:btnTempFrame];
[btnTemp setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[btnTemp.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[btnTemp.titleLabel setTextColor:[UIColor whiteColor]];
[btnTemp addTarget:self action:#selector(onDownloadClick:) forControlEvents:UIControlEventTouchUpInside];
CGRect btnSubscribeFrame=CGRectMake(100, 385, 90, 30);
UIButton *btnSubscribe=[[UIButton alloc]initWithFrame:btnSubscribeFrame];
[btnSubscribe setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[btnSubscribe.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[btnSubscribe.titleLabel setTextColor:[UIColor whiteColor]];
[btnSubscribe setTitle:#"Subscribe" forState:UIControlStateNormal];
[btnSubscribe addTarget:self action:#selector(moveTosubscribe:) forControlEvents:UIControlEventTouchUpInside];
CGRect closeButtonFrame=CGRectMake(195, 385, 60, 30);
UIButton *closeButton=[[UIButton alloc]initWithFrame:closeButtonFrame];
[closeButton setBackgroundImage:[UIImage imageNamed:#"btn_plain_iphone.png"] forState:UIControlStateNormal];
[closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[closeButton.titleLabel setTextColor:[UIColor whiteColor]];
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
CGRect swipeLabelFrame=CGRectMake(0, 0, 140, 15);
UILabel *swipeLabel=[[UILabel alloc]initWithFrame:swipeLabelFrame];
[swipeLabel setBackgroundColor:[UIColor clearColor]];
[swipeLabel setText:#"> SWIPE TO SEE MORE"];
[swipeLabel setTextColor:[UIColor lightGrayColor]];
[swipeLabel setFont:[UIFont systemFontOfSize:12]];
CGRect dateLabelFrame=CGRectMake(5, 365, 100, 15);
dateLabel=[[UILabel alloc]initWithFrame:dateLabelFrame];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setText:#"July 2013 #93"];
[dateLabel setTextColor:[UIColor whiteColor]];
[dateLabel setFont:[UIFont systemFontOfSize:13]];
CGRect priceLabelFrame=CGRectMake(155, 365, 100, 15);
priceLabel=[[UILabel alloc]initWithFrame:priceLabelFrame];
[priceLabel setBackgroundColor:[UIColor clearColor]];
[priceLabel setText:#"$3.9"];
[priceLabel setTextColor:[UIColor colorWithRed:71.0/255 green:191.0/255 blue:226.0/255 alpha:1]];
[priceLabel setFont:[UIFont systemFontOfSize:13]];
[priceLabel setTextAlignment:NSTextAlignmentRight];
CGRect labelFrame=swipeLabel.frame;
labelFrame.origin=CGPointMake(popUpView.frame.origin.x+popUpView.frame.size.width - labelFrame.size.width, popUpView.frame.origin.y - labelFrame.size.height);
[swipeLabel setFrame:labelFrame];
[popUpView addSubview:imageGalleryView];
[popUpView addSubview:btnTemp];
[popUpView addSubview:closeButton];
[popUpView addSubview:btnSubscribe];
[popUpView addSubview:dateLabel];
[popUpView addSubview:priceLabel];
[UIView beginAnimations:#"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:popUpView cache:YES];
[self addSubview:popUpView];
[self addSubview:swipeLabel];
[UIView commitAnimations];
UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
self.backgroundColor = color;
[[[UIApplication sharedApplication] keyWindow] addSubview:self];
[self initializeVariables];
}
return self;
}
And in Parent View controller just create object of UIView Class and call it with initWithFrame and size which you want
ModalPreviewViewController_iPhone *mpvc=[ModalPreviewViewController_iPhone alloc];
if(appDelegate.isIphone5)
mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,480)];
else
mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,568)];
And for dismissing UivewClass
write just following sentence to dismiss it ,in one of its(UIviewClass's) method like closeView
[self removeFromSuperview];
And let me know if you face any problem

I am trying to set background image to UISearchBar but background image is not getting displaying in my iPhone application

In my iPhone app I am trying to set background image to UISearch Bar, my image is not getting displayed.
Here is my code
UISearchBar * tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 40 )];
self.searchBar = tempSearchBar;
tempSearchBar.delegate = self;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.barStyle = UIBarStyleBlack;
self.searchBar.backgroundColor=[UIColor clearColor];
self.searchBar.backgroundImage=[UIImage imageNamed:#"Searchbox.png"];
self.searchBar.placeholder = #"Search My Data";
[self.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
[tempSearchBar release];
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"Searchbox.png"] forState:UIControlStateNormal];
By using above method I can set bg Image for searchBar
[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"search_bar"] forState:UIControlStateNormal];
While Interface Builder provides the equivalent functionality of the following code (not the desired effect though)
[self.searchDisplayController.searchBar setBackgroundImage:[UIImage imageNamed:#"search_bar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
(void) clearSearchBarBg
{
for (UIView *subview in searchBarCity.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
for (UIView *subview in searchBarCity.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
}
}
for(UIView *subView in searchBarCity.subviews)
{
if([subView isKindOfClass: [UITextField class]])
{
[(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];
}
}
UITextField *searchField = [searchBarCity valueForKey:#"_searchField"];
[searchField setBackground:[UIImage imageNamed:#"SearchInputBox.png"]];// = [UIColor clearColor];
searchField.borderStyle = UITextBorderStyleNone;
[searchField setTextColor:[UIColor blackColor]];
UIImageView* image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
image.image = [UIImage imageNamed:#"navigationBarColorCode.png"];
image.userInteractionEnabled = FALSE;
[searchBarCity insertSubview:image belowSubview:searchField];
searchBarCity.userInteractionEnabled = YES;
searchBarCity.placeholder = #"Search";
}
Use below code
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"search_box.png"] forState:UIControlStateNormal];
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.tintColor=[UIColor orangeColor];
Set the background image of the SearchBar:
[self.searchController.searchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
then change SearchField Background Image:
[self.searchController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"Searchbox.png"] forState:UIControlStateNormal];
If you don't want to show the Search Icon in the search bar then do the following:
UITextField *searchField = (UITextField *)[self.searchController.searchBar valueForKey:#"searchField"];
searchField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LEFT_VIEW_WIDTH, searchField.frame.size.height)];

Resources