I have created a custom tabbar and added a custom image at the center position. The following code works as expected.It adds my custom button at the center of the tabbar bar.
CustomTabBarViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* buttonImage = [UIImage imageNamed:#"icon_floating.png"];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[button addTarget:self
action:#selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) myAction
{
self.selectedIndex = 2;
}
The issue that I have faced as follows. There is a viewcontroller - ChatViewContoller where I hide tabbar. But since the custom button is added on the tabbar, I could not able to hide the custom button with the following code.
ChatViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
tab.button.hidden = YES;
}
Hotspring,please reference the original tabbar object instead of creating a new one while hiding center button i.e
CustomTabBarViewController* tab = //reference to existing tabbar
instead of
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
and then try hiding button
Related
Like the Instagram app, i would like to have my center tab bar item a specific color.
I understand this is how I would set the selected image:
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:0.484 green:0.403 blue:0.884 alpha:1] forSize:CGSizeMake(64, 49) withCornerRadius:0]];
But how would I keep the 3rd tab's background to always stay that color?
Also, how do I make the middle tab separate from the rest, as in, when I press the center tab, there is modal presentation of a view controller (like instagram), and the previous tab stays selected for when dismissing the modal view controller.
#interface BaseViewController : UITabBarController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage: (UIImage*)highlightImage;
#implementation BaseViewController
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init] ;
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
Subclass this Tab controller and call the above function to set image o tab bar items and center button like Instagram
#interface InstagramViewController : BaseViewController
#end
#implementation InstagramViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllers = [NSArray arrayWithObjects:
[self viewControllerWithTabTitle:#"Feed" image:[UIImage imageNamed:#"112-group.png"]],
[self viewControllerWithTabTitle:#"Popular" image:[UIImage imageNamed:#"29-heart.png"]],
[self viewControllerWithTabTitle:#"Share" image:nil],
[self viewControllerWithTabTitle:#"News" image:[UIImage imageNamed:#"news.png"]],
[self viewControllerWithTabTitle:#"#user" image:[UIImage imageNamed:#"123-id-card.png"]], nil];
}
-(void)willAppearIn:(UINavigationController *)navigationController
{
[self addCenterButtonWithImage:[UIImage imageNamed:#"cameraTabBarItem.png"] highlightImage:nil];
}
#end
I am new to using MapView in iOS, here's my situation:
When I toggle the nearby button it will redirect me to map view:
Then when I toggle the List View button, it will display a UItableView with a Map Button. Then finally, when I toggle the map button it will display the Map View.
My problem is, when in list view then I scrolled down then toggle the map view button the map view doesn't fill the whole screen (please see image for reference):
Here's what i have so far:
This is how i initiate the Map:
- (void)viewDidLoad {
jobsMapView = [[MTJobsMapView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
jobsMapView.showsUserLocation = YES;
jobsMapView.zoomEnabled = YES;
jobsMapView.rotateEnabled = NO;
jobsMapView.parentViewController = self;
jobsMapView.delegate = jobsMapView;
// Set up buttons but hide them
showListViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
showListViewButton.frame = CGRectMake(self.view.frame.size.width-58, self.view.frame.size.height-159, 50.0, 36.0);
[showListViewButton addTarget:self action:#selector(toggleNearbyView:) forControlEvents:UIControlEventTouchUpInside];
[showListViewButton setImage:[UIImage imageNamed:#"list.png"] forState:UIControlStateNormal];
[self.view addSubview:showListViewButton];
showMapViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
showMapViewButton.frame = CGRectMake(self.view.frame.size.width-58, self.view.frame.size.height-159, 50.0, 36.0);
[showMapViewButton addTarget:self action:#selector(toggleNearbyView:) forControlEvents:UIControlEventTouchUpInside];
[showMapViewButton setImage:[UIImage imageNamed:#"icon_map.png"] forState:UIControlStateNormal];
[self.view addSubview:showMapViewButton];
// Default mapView is shown in Nearby View
showListViewButton.hidden = YES;
showListViewButton.enabled = NO;
showMapViewButton.hidden = YES;
showMapViewButton.enabled = NO;
self.listViewSelected = NO;
}
- (IBAction)toggleNearbyView:(UIButton *)sender {
self.listViewSelected = !self.listViewSelected;
if (self.listViewSelected) {
self.title=[NSString stringWithFormat:#"Nearby Jobs (%d)",[[self.jobsDictionary objectForKey:#"sub_slots"] count]];
// TableView is shown, so showMapViewButton for user to select MapView
showMapViewButton.hidden = NO;
showMapViewButton.enabled = YES;
showListViewButton.hidden = YES;
showListViewButton.enabled = NO;
[jobsMapView removeFromSuperview];
} else {
showListViewButton.hidden = NO;
showListViewButton.enabled = YES;
showMapViewButton.hidden = YES;
showMapViewButton.enabled = NO;
self.title=[NSString stringWithFormat:#"Nearby Jobs (%d)",[[self.FilterDictionary objectForKey:#"sub_slots"] count]];
[jobsMapView mapViewWithJobsDisplayed:self.FilterDictionary andUserLocation:xapp.self.userLocation andUserCoordinates:xapp.self.userLocationCoordinates];
[self.jobsTableView addSubview:jobsMapView];
}
}
what am doing wrong here?
Any help will be appreciated..
thanks...
Comment This line.
self.listViewSelected = !self.listViewSelected;
in toggleNearbyView method
I am looking to recreate the raised tab bar as seen in many apps but am looking to do this in a universal app where the raised position would need to change.
At the moment I am not having much luck creating this so have come here for enlightenment, failing this I may just create a custom tab bar background.
If this is the case, would this work in the app delegate to differentiate the two devices?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:#"iPadtabBar.png"]];
}
else {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:#"iPhonetabBar.png"]];
}
You may be thinking, why doesn't he just test it.. well that would be easier and if I could I would rather than ask first but I don't get my Mac back until the start of next week!
You will have to subclass UITabBarController and customize that center tab button. There is a nicely done example here: https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar/Classes
Header file:
#interface CustomTabBarViewController : UITabBarController{
}
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage;
#end
Implementation file:
#implementation CustomTabBarViewController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init];
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
So I'm having issues with the buttons for the filters in my photo app. I'm not exactly sure why, but they've suddenly stopped working. The selector is not being called when they're tapped, but I have no idea why. They were working just fine a few days ago, but for some reason they're not now. I've checked the UIView subviews array, verified that it's right on top. I need a way to see if, for some reason, the touches are not making it to the button. I'm not sure how to do this.
I wish I had more information to give, but this is all I've got. I hope someone has some suggestions because I'm at wit's end with it.
Thanks in advance!
Button Creation Method:
-(void)createFilterButtonsForThumbnail:(UIImage *)thumbnail
{
UIView *filtersContainer = self.filterContainer;
CGRect buttonFrame = CGRectMake(kFilterFrameThickness, kFilterFrameThickness,
thumbnail.size.width, thumbnail.size.height);
CGFloat frameWidth = thumbnail.size.width+(2*kFilterFrameThickness);
CGFloat frameHeight = kFilterPickerHeight;
UIEdgeInsets backgroundInsets = UIEdgeInsetsMake(0, kFilterFrameThickness, 0, kFilterFrameThickness);
UIImage *buttonBackgroundImage = [[UIImage imageNamed:#"FilmReel"] resizableImageWithCapInsets:backgroundInsets];
for (int i = 0;i<(self.filterPaths.count+kFilterNonLookups+1);i++){
UIImageView *buttonBackground = [[UIImageView alloc] initWithFrame:CGRectMake(kFilterSidePadding+(i*(frameWidth+kFilterSidePadding)),
0,
frameWidth,
frameHeight)];
[buttonBackground setImage:buttonBackgroundImage];
UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:buttonFrame];
UIImage *filteredThumbnail = [self applyFilterAtIndex:i ToImage:thumbnail];
[thumbnailButton setImage:filteredThumbnail forState:UIControlStateNormal];
[thumbnailButton addTarget:self action:#selector(filterSelected:) forControlEvents:UIControlEventTouchUpInside];
[thumbnailButton setTag:i];
[buttonBackground addSubview:thumbnailButton];
[filtersContainer addSubview:buttonBackground];
if ((i > (kFilterProMinimumIndex)) && ([self isProVersion]) == NO){
UIImageView *proTag = [[UIImageView alloc] initWithImage:nil];
CGRect proFrame = CGRectMake(buttonFrame.origin.x,
buttonFrame.origin.y + buttonFrame.size.height - kFilterProIconHeight-kFilterFrameThickness,
kFilterProIconWidth,
kFilterProIconHeight);
[proTag setBackgroundColor:[UIColor orangeColor]];
[proTag setFrame:proFrame];
[thumbnailButton addSubview:proTag];
[self.filterProTags addObject:proTag];
}
}
}
Selector Method:
-(void)filterSelected:(UIButton *)button
{
NSLog(#"Pressed button for index %i",button.tag);
int buttonTag = button.tag;
if ((buttonTag < kFilterProMinimumIndex+1) || ([self isProVersion] == YES)){
[self.imageView setImage:[self applyFilterAtIndex:buttonTag ToImage:self.workingImage]];
}
else {
[self processProPurchaseAfterAlert];
}
}
I see a couple issues in this, but I'm not sure which ones are causing it to break. First, you should instantiate your button using buttonWithType: on UIButton:
UIButton *thumbnailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbnailButton setFrame:buttonFrame]
The reason for this is UIButton is a class cluster, and you should let the OS give you the correct button.
Secondly, you're adding the Button as a Subview of an UIImageView, which by default, has userInteractionEnabled set to NO.
This property is inherited from the UIView parent class. This class
changes the default value of this property to NO.
You should have the button be one large button, with the background of the button be the image you want it to be.
I'm working on an iOS app that requires the user to enter numbers into a UITextField using the keyboard type UIKeyboardTypeDecimalPad. However I just realized that there is no support for entering negative numbers, which is a requirement of the application.
Any ideas or thoughts on how I can accomplish this?
You can use a UIToolbar as an input accessory view for your UITextField and put a button with a "+/-" (plus/minus sign in it).
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *plusMinusBbi = [[UIBarButtonItem alloc]initWithTitle:#"+/-" style:UIBarButtonItemStylePlain target:self action:#selector(togglePositiveNegative:)];
toolbar.items = #[plusMinusBbi];
self.textField.inputAccessoryView = toolbar;
I don't believe that something like that is possible right now (since Apple hasn't implemented it yet). The only option would be to design your own keyboard or use a complete ASCII one.
func addToolbar() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let plusMinusButton = UIBarButtonItem(title: "+/-", style: .done, target: self, action: #selector(plusMinusAction))
plusMinusButton.tintColor = .black
plusMinusButton.width = UIScreen.main.bounds.width / 3
toolbar.items = [plusMinusButton]
toolbar.barTintColor = #colorLiteral(red: 0.7812563181, green: 0.8036255836, blue: 0.8297665119, alpha: 1)
toolbar.isTranslucent = false
myField.inputAccessoryView = toolbar
}
#objc func plusMinusAction() {
let text = myField.text ?? ""
if text.hasPrefix("-") {
myField.text = String(text.suffix(text.count - 1))
} else {
myField.text = "-\(text)"
}
}
From what I've found apple has yet to implement such a standard keyboard. However it is possible to add UIButtons to any keyboard window. this link should help, or similar tutorial this link should also help
Basically you register a NSNotificationListener to listen for the keyboard coming up. Grab the keyboards frame and add a UIButton to its view. The above link isn't quite what we want but its the right idea.
In appDelegate,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
- (void)keyboardDidShow:(NSNotification *)note
{
// Get the Very Top Window on the Display. That's where the Keyboard is.
NSInteger topWindow = [[[UIApplication sharedApplication] windows] count] - 1;
UIWindow *keyboard = [[[UIApplication sharedApplication] windows] objectAtIndex:topWindow];
// If the dot has not been created (first time the keyboard has been displayed) create it.
if (self.dot == nil)
{
self.dot = [UIButton buttonWithType:UIButtonTypeCustom];
// Make the dot a subview of the view containing the keyboard.
[keyboard addSubview:self.dot];
// Place the dot in the correct location on the keyboard.
[self.dot setFrame:CGRectMake(0, 427, 106, 53)];
// Set the overlay graphics. (Use TransDecimalDown.png and TransDecimalUp.png for the Alert Style Keyboard.
[self.dot setImage:[UIImage imageNamed:#"DecimalUp.png"] forState:UIControlStateNormal];
[self.dot setImage:[UIImage imageNamed:#"DecimalDown.png"] forState:UIControlStateHighlighted];
// Give the dot something to do when pressed.
[self.dot addTarget:self action:#selector(sendDecimal:) forControlEvents:UIControlEventTouchUpInside];
}
// Bring the dot to the front of the keyboard.
[keyboard bringSubviewToFront:self.dot];
}
- (void)sendDecimal:(id)sender {
// Post a notification that the dot has been pressed. Observing view controllers are then responsible for adding the actual decimal.
[[NSNotificationCenter defaultCenter] postNotificationName:#"DecimalPressed" object:nil];
// Play the Keyboard Click. If the user has these sound effects turned off, the decimal will still click. Sorry. :( (Also, doesn't seem to work on the simulator, no keyboard clicks seem to.)
AudioServicesPlaySystemSound(0x450);
}
sorry for the horrible code format but you get the idea :)
This clearly isn't my best answer.. but i'm unable to delete it since it is accepted.
may this code will help you:
if you want a negative number just use "-"
NSString *fieldString = [NSString stringWithFormat:#"%#",Textfield.text];
NSLog(#"%#",fString);
int fieldValue;
value = [fString intValue];
NSLog(#"%d",fieldValue);
this will work for decimal numbers
double fieldValue;
value = [fString doubleValue];
NSLog(#"%f",fieldValue);
To accomplish the task I came using '.inputAccessoryView' with the textfield
On viewDidLoad
self.textField.inputAccessoryView = [self accessoryViewForTextField:self.textField];
then
- (UIView *)accessoryViewForTextField:(UITextField *)textField{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
view.backgroundColor = [UIColor lightGrayColor];
UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[minusButton setTitle:#"-" forState:UIControlStateNormal];
[doneButton setTitle:NSLocalizedString(#"Done", #"Done") forState:UIControlStateNormal];
minusButton.backgroundColor = [UIColor magentaColor];
doneButton.backgroundColor = [UIColor blueColor];
CGFloat buttonWidth = view.frame.size.width/3;
minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);
[minusButton addTarget:self action:#selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:self action:#selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:minusButton];
[view addSubview:doneButton];
return view;
}
this will add a custom view just above the keyboard as a part of it
finally to get the 'minus'
#pragma mark - IBActions
- (IBAction)minusTouchUpInside:(id)sender
{
NSString *value = self.textField.text;
if (value.length > 0) {
NSString *firstCharacter = [value substringToIndex:1];
if ([firstCharacter isEqualToString:#"-"]){
self.textField.text = [value substringFromIndex:1];
}else{
self.textField.text = [NSString stringWithFormat:#"-%#", value];
}
}
}
- (IBAction)doneTouchUpInside:(id)sender
{
[self.textField resignFirstResponder];
}