how to add UITabBarItem programmatically? - ios

In my application, i have added UITabBarController in "Storyborad".
But now i want to add UITabBarItem programmatically. Let's say there are 5 buttons and when ever user click on that button my UITabBarController is called.
There are total 5 tab in "tabbar".
e.g:
Tab name:item1,item2,item3,item4,item5.
Button name:item1,item2,item3,item4,item5.
Let's say user click on item1 button, UITabBarcontroller is called. Now user should not be able to see that item1 tab in "tabbarcontroller", he should be able to see the reset "tabBaritem".
Can anyone help me out with this?
How to do it programmatically?
Thanks.
Nilesh J

UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:#"Page 1"
image:[UIImage imageNamed:#"page1_image_normal"]
selectedImage:[UIImage imageNamed:#"page1_image_selected"]];
Get existing tabBarItems
NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];
//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];
Swift 4
if var controllers = tabBarController?.viewControllers {
let tabItem = UITabBarItem(title: "Item \(controllers.count)", image: nil, selectedImage: nil)
let vc = UIViewController() // your new view controller
vc.tabBarItem = tabItem
controllers.append(vc) // or insert at index up to you
tabBarController?.setViewControllers(controllers, animated: true)
}

To call tabBar click event you need to do the following:
call delegatemethod on UITabBarDelegate and implement their method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//self.tabBarItem.title = #"Title";
}

This is for Custom Tabbar Controller. In this code you can give your Tabbar Button Image and when you click one Button that buttom will be changed according to your image.
Set Delegate Method for UITabBarControllerDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self creationTabBar];
[self.window makeKeyAndVisible];
return YES;
}
-(void)creationTabBar
{
UINavigationController *myNavigationController;
// Create initialized instance of UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height);
// Create initialized instance of NSMutableArray to hold our UINavigationControllers
NSMutableArray *tabs = [[NSMutableArray alloc] init];
World_ViewController *WorldScreen;
Home_ViewController *HomeScreen;
Favourite_ViewController *FavouriteScreen;
Rank_ViewController *RankScreen;
Setting_ViewController *SettingScreen;
UILabel *item1, *item2, *item3, *item4, *item5;
// Create first UIViewController
WorldScreen = [[World_ViewController alloc] initWithNibName:#"World_ViewController" bundle:nil];
HomeScreen = [[Home_ViewController alloc] initWithNibName:#"Home_ViewController" bundle:nil];
FavouriteScreen= [[Favourite_ViewController alloc] initWithNibName:#"Favourite_ViewController" bundle:nil];
RankScreen = [[Rank_ViewController alloc] initWithNibName:#"Rank_ViewController" bundle:nil];
SettingScreen = [[Setting_ViewController alloc] initWithNibName:#"Setting_ViewController" bundle:nil];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
item1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 430, 64, 50)];
item2 = [[UILabel alloc] initWithFrame:CGRectMake(64, 430, 64, 50)];
item3 = [[UILabel alloc] initWithFrame:CGRectMake(128, 430, 64, 50)];
item4 = [[UILabel alloc] initWithFrame:CGRectMake(195, 430, 64, 50)];
item5 = [[UILabel alloc] initWithFrame:CGRectMake(256, 430, 64, 50)];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:HomeScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:WorldScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:FavouriteScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:RankScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:SettingScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[tabBarController setViewControllers:tabs];
[tabBarController setDelegate:self];
[item1 setText:#"item1"];
[item1 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item1];
item1.tag = 101;
[item2 setText:#"item2"];
[item2 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item2];
item2.tag = 102;
[item3 setText:#"item3"];
[item3 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item3];
item3.tag = 103;
[item4 setText:#"item4"];
[item4 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item4];
item4.tag = 104;
[item5 setText:#"item5"];
[item5 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item5];
item5.tag = 105;
[self.window setRootViewController:self.viewController];
}
- (void)tabBarController:(UITabBarController *)tabBarControllerref didSelectViewController:(UIViewController *)viewController
{
UILabel *item1 = (UILabel*)[tabBarControllerref.view viewWithTag:101];
UILabel *item2 = (UILabel*)[tabBarControllerref.view viewWithTag:102];
UILabel *item3 = (UILabel*)[tabBarControllerref.view viewWithTag:103];
UILabel *item4 = (UILabel*)[tabBarControllerref.view viewWithTag:104];
UILabel *item5 = (UILabel*)[tabBarControllerref.view viewWithTag:105];
[item1 setFrame: CGRectMake(0, 430, 64, 50)];
[item2 setFrame: CGRectMake(64, 430, 64, 50)];
[item3 setFrame: CGRectMake(128, 430, 64, 50)];
[item4 setFrame: CGRectMake(195, 430, 64, 50)];
[item5 setFrame: CGRectMake(256, 430, 64, 50)];
if([tabBarControllerref selectedIndex] == 0)
{
[item1 setTextColor:[UIColor redColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 1)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor redColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 2)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor redColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 3)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor redColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 4)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor redColor]];
}
}

Related

UIAlertController is not is window hierarchy

Im trying to show up a pop up containing a UIPickerView and UITextField.
Following is my code that I use to create the pop up.
self.alert = [UIAlertController alertControllerWithTitle:nil message:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
CGRect pickerFrame = CGRectMake(0, 0, 270, 200);
UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
regionsPicker.tag = 1;
regionsPicker.dataSource = (id <UIPickerViewDataSource>)self;
regionsPicker.delegate = (id <UIPickerViewDelegate>) self;
[regionsPicker setShowsSelectionIndicator:YES];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);
UIImage *btnImage = [UIImage imageNamed:#"delete.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
[toolView addSubview:button];
[button addTarget:self action:#selector(closeDialog:) forControlEvents:UIControlEventTouchUpInside];
self.textField = [[UITextView alloc] initWithFrame:CGRectMake(30, 170, 220, 200)];
_textField.editable = NO;
_textField.hidden=true;
self.textField.text = #"Enter text...";
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.textColor = [UIColor lightGrayColor];
self.textField.delegate = (id <UITextViewDelegate>) self;
self.textField.layer.borderWidth = 1.0f;
self.textField.layer.cornerRadius = 5;
self.textField.layer.borderColor = [[UIColor grayColor] CGColor];
[toolView addSubview:self.textField];
CGRect actionFrame = CGRectMake(80, 400, 100, 30);
UIButton *actionBtn = [[UIButton alloc] initWithFrame: actionFrame];
[actionBtn setTitle:#"Submit" forState:UIControlStateNormal];
[actionBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[actionBtn setBackgroundColor:[UIColor purpleColor]];
[self.textField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[self.alert.view setUserInteractionEnabled:YES];
[self.alert.view addSubview:regionsPicker];
[self.alert.view addSubview:self.textField];
[self.alert.view addSubview:toolView];
[self.alert.view addSubview:actionBtn];
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:_alert animated:YES completion:nil];
It works fine.However, if Im trying to enter text in the UITextField by tapping it, the keyboard doesn't appear and hence Im not be able to type anything within the UITextField. How can I sort this out?
the reason is you added textField, button, all those inside toolview , but the toolview height is very less
change your toolView height from 44
into 500 or else (combination of textfield, button + 50).
Change below statement
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
to
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];
Update
change this line
[self presentViewController:alert animated:YES completion:nil];
into
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Update New
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}else
{
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Swift
var rootViewController: UIViewController = UIApplication.sharedApplication().delegate.window.rootViewController
if (rootViewController is UINavigationController.self) {
rootViewController = ((rootViewController as! UINavigationController)).viewControllers[0]
}
else {
while rootViewController.presentedViewController {
rootViewController = rootViewController.presentedViewController
}
}
rootViewController.presentViewController(alert, animated: true, completion: { _ in })
Try this it may help you:-
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:#" Your message " preferredStyle:UIAlertControllerStyleAlert];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:#"Image" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[toolView addSubview:button];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 44)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.backgroundColor = [UIColor grayColor];
txtField.font = [UIFont systemFontOfSize:15];
txtField.placeholder = #"enter text";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = (id<UITextFieldDelegate>)self;
[toolView addSubview:txtField];
[txtField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[alertController.view setUserInteractionEnabled:YES];
[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];

UIToolbar UILabel x y offset not working

I encountered a strange problem. I need to put two UILabel in UIToolbar. I have a button and I want to share with respect to its center the UILabel, as if I did not change the values of x and y inside UILabel up at the bottom, they do not move. Tell me what could be the problem. I need a little lower down the UILabel.
= [NSMutableArray new];
_createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 20)];
_createdLabel.backgroundColor = [UIColor clearColor];
_createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
_createdLabel.font = myFont;
_createdLabel.text = #"";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:_createdLabel];
[_items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[_items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, -1000, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing:)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[_items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing:)];
shareBtn.tintColor = [UIColor whiteColor];
[_items addObject:shareBtn];
[self.toolBar setItems:_items animated:YES];
Please remove [shareLabel sizeToFit]; in your code.
Use below code for share label.
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 4, 85, 16)];
You try this code, it's working fine
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
[toolBar setBarTintColor: self.navigationController.navigationBar.tintColor];
NSMutableArray *items= [NSMutableArray new];
UILabel *createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 20)];
createdLabel.backgroundColor = [UIColor clearColor];
createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
createdLabel.font = myFont;
createdLabel.text = #"Hai";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:createdLabel];
[items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Hello "; //#"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing)];
shareBtn.tintColor = [UIColor whiteColor];
[items addObject:shareBtn];
[toolBar setItems:items animated:YES];
[self.view addSubview:toolBar];
provide method like this,
-(void)socialSharing
{
NSLog(#"Hai");
}
Definitely helps to you.

iOS- Objective C - Show a view on ipad only but hide on iPhone

I need a solution for the problem described below..
I have a scenario where a view needs to be shown only on ipad but it needs to be hidden on iphone. The way the view is built is as follows:
The below code shows the view on ipad. I want to hide this view when loaded on iphone.
How can this be achieved?
(void)viewDidLoad
{
[super viewDidLoad];
gotQuoteList = NO;
isExpanded = NO;
heightOfUploadManager = 282;
freqQuotesArray = [[NSMutableArray alloc] init];
selectedCheckboxes = [[NSMutableDictionary alloc] init];
serviceConfig_G_Obj = [[ServiceConfig alloc] init];
cacheManager_G_Obj = [[CacheManager alloc] init];
[self.view setBackgroundColor: [[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
[self observeRotationChanges];
uploadMngrView = [[UIView alloc] init];
[self setUploadManagerFrame];
[uploadMngrView setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
uploadMngrView.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadMngrView.layer.borderWidth = 1.0;
[self.view addSubview:uploadMngrView];
UIView *titleLblBgView = [[UIView alloc] init];
[titleLblBgView setFrame:CGRectMake(0, 40, 512, 1)];
[titleLblBgView setBackgroundColor:[self colorWithHexString:#"C7C7C7"]];
[uploadMngrView addSubview:titleLblBgView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 1, 512-21, 38)];
[titleLabel setText:#"Upload Document"];
[titleLabel setTextAlignment:NSTextAlignmentLeft];
[titleLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[titleLabel setTextColor:[self colorWithHexString:#"656565"]];
[titleLabel setFont:[UIFont fontWithName:#"Arial" size:21]];
[uploadMngrView addSubview:titleLabel];
UILabel *fileLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 141, 34)];
[fileLabel setText:#"File"];
[fileLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[fileLabel setTextColor:[self colorWithHexString:#"656565"]];
[fileLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:fileLabel];
UILabel *fileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 60, 329, 34)];
[fileNameLabel setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]];
[fileNameLabel setTextColor:[self colorWithHexString:#"656565"]];
[fileNameLabel setText:[NSString stringWithFormat:#" %#",[[self getFileName] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[fileNameLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
fileNameLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
fileNameLabel.layer.borderWidth = 1.0;
[uploadMngrView addSubview:fileNameLabel];
UILabel *destinationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 114, 141, 34)];
[destinationLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationLabel setText:#"Destination"];
[destinationLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[destinationLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:destinationLabel];
UILabel *destinationNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 114, 329, 34)];
destinationNameLabel.tag = 1;
UITapGestureRecognizer *worksaceRBTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapRadioBtnLabelWithGesture:)];
worksaceRBTapGestureRecognizer.numberOfTapsRequired = 1;
[destinationNameLabel setUserInteractionEnabled:YES];
[destinationNameLabel addGestureRecognizer:worksaceRBTapGestureRecognizer];
destinationNameLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
destinationNameLabel.layer.borderWidth = 1.0;
[destinationNameLabel setText:#" My Workspace"];
[destinationNameLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]]; [destinationNameLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationNameLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:destinationNameLabel];
myWorkspaceRadioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
myWorkspaceRadioBtn.tag = 1;
[myWorkspaceRadioBtn setImage:[UIImage imageNamed:#"radiobtn_off.png"] forState:UIControlStateNormal];
[myWorkspaceRadioBtn setImage:[UIImage imageNamed:#"radiobtn_on.png"] forState:UIControlStateSelected];
[myWorkspaceRadioBtn setFrame:CGRectMake(166, 121, 26, 22)];
myWorkspaceRadioBtn.selected=YES;
[myWorkspaceRadioBtn addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:myWorkspaceRadioBtn];
UILabel *destinationQuoteLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 147, 329, 34)];
destinationQuoteLabel.tag = 2;
UITapGestureRecognizer *quotelistRBTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapRadioBtnLabelWithGesture:)];
quotelistRBTapGestureRecognizer.numberOfTapsRequired = 1;
[destinationQuoteLabel setUserInteractionEnabled:YES];
[destinationQuoteLabel addGestureRecognizer:quotelistRBTapGestureRecognizer];
destinationQuoteLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
destinationQuoteLabel.layer.borderWidth = 1.0;
[destinationQuoteLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationQuoteLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[destinationQuoteLabel setText:#" Link to selected quote"];
[destinationQuoteLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]];
[uploadMngrView addSubview:destinationQuoteLabel];
quoteListRadioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
quoteListRadioBtn.tag = 2;
[quoteListRadioBtn setImage:[UIImage imageNamed:#"radiobtn_off.png"] forState:UIControlStateNormal];
[quoteListRadioBtn setImage:[UIImage imageNamed:#"radiobtn_on.png"] forState:UIControlStateSelected];
[quoteListRadioBtn setFrame:CGRectMake(166, 152, 26, 22)];
[quoteListRadioBtn addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:quoteListRadioBtn];
[self fillQuoteListScrollView];
lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 202, 512, 1)];
lineView.backgroundColor = [self colorWithHexString:#"C7C7C7"];
[uploadMngrView addSubview:lineView];
cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(20, 222, 231, 40)];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[cancelButton setTitleColor:[self colorWithHexString:#"656565"] forState:UIControlStateNormal];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
CAGradientLayer *cancelBtnGradient = [CAGradientLayer layer];
cancelBtnGradient.frame = cancelButton.bounds;
cancelBtnGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:255.0/255.0f green:255.0/255.0f blue:255.0/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:238.0/255.0f green:238.0/255.0f blue:238.0/255.0f alpha:1.0f] CGColor], nil];
[cancelButton.layer insertSublayer:cancelBtnGradient atIndex:0];
cancelButton.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
cancelButton.layer.borderWidth = 1.0;
[cancelButton addTarget:self action:#selector(cancelUpload) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:cancelButton];
uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadButton setFrame:CGRectMake(261, 222, 231, 40)];
[uploadButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[uploadButton setTitleColor:[self colorWithHexString:#"55A0B9"] forState:UIControlStateNormal];
[uploadButton setTitle:#"Upload" forState:UIControlStateNormal];
CAGradientLayer *uploadBtnGradient = [CAGradientLayer layer];
uploadBtnGradient.frame = uploadButton.bounds;
uploadBtnGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:255.0/255.0f green:255.0/255.0f blue:255.0/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:238.0/255.0f green:238.0/255.0f blue:238.0/255.0f alpha:1.0f] CGColor], nil];
[uploadButton.layer insertSublayer:uploadBtnGradient atIndex:0];
uploadButton.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadButton.layer.borderWidth = 1.0;
[uploadButton addTarget:self action:#selector(uploadDoc) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:uploadButton];
}
You can do this like this,
uploadMngrView = [[UIView alloc] init];
[self setUploadManagerFrame];
[uploadMngrView setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
uploadMngrView.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadMngrView.layer.borderWidth = 1.0;
if ([UIDevice currentDevice].userInterfaceIdom == UIUserInterfaceIdoniPad) {
[self.view addSubview:uploadMngrView];
}
HTH, Enjoy Coding !!
As your setting up the views programatically, you something like this
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Create the view only for ipad
self.someView = [[UIView alloc] initWithFrame:someFrame];
//Or set the view hidden just on ipad
self.someView.hidden = YES;
//Or set the view a different frame on ipad
self.someView.frame = CGRectMake(x,y,width,height);
}
else
{
//Do some non ipad layout stuff here
}

Why my UIButton doesn't display in my subview? iPad App

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

Label keeps disappearing from my navigation menu

For some reason the label in my navigation bar keeps disappearing and only displaying my map icon and home button. Is it because I'm adding it as a barbuttonitem? Is there a way around this?
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(160, 5, 250, 70)];
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0,5, 310, 70)];
l1.text=#"FIND THINGS TO DO";
l1.font=[UIFont fontWithName:#"Helvetica Neue LT Std" size:25.0];
l1.backgroundColor=[UIColor clearColor];
l1.textColor=[UIColor whiteColor];
l1.textAlignment=UITextAlignmentCenter;
[view1 addSubview:l1];
UIBarButtonItem *left2=[[UIBarButtonItem alloc]initWithCustomView:view1];
self.navigationItem.rightBarButtonItem=left2;
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBarHidden = NO;
UIImage* image3 = [UIImage imageNamed:#"home_btn.png"];
homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 38, 38)];
[homeBtn setBackgroundImage:image3 forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(Home)
forControlEvents:UIControlEventTouchUpInside];
[homeBtn setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
NSArray *arr=[[NSArray alloc]initWithObjects:home, nil];
self.navigationItem.rightBarButtonItems = arr;
UIImage* mapButton1 = [UIImage imageNamed:#"Top-Map-Icon.png"];
mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 30)];
[mapButton setBackgroundImage:mapButton1 forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(Map)
forControlEvents:UIControlEventTouchUpInside];
[mapButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *map = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
NSArray *arr2 = [[NSArray alloc] initWithObjects:map, nil];
self.navigationItem.leftBarButtonItems = arr2;

Resources