After searching through several question on StackOverflow I've found out that there is only 1 major project for creating custom UITabBar called BCTabBarController. The description to it says:
There are several problems with using the standard UITabBarController
including:
It is too tall, especially in landscape mode
The height doesn't match the UIToolbar
It cannot be customized without using private APIs
Nevertheless, I've found this strange project on GitHub with the tutorial here that uses standard UITabBarController in its implementation with UIButtons for each tab and it's working (strangely enough, but it does).
I was wondering, if this is wrong to create your custom UITabBarController with UIButtons instead of tabs and what would it result into? The implementation of this looks like this:
- (void)viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideTabBar];
[self addCustomElements];
}
- (void)hideTabBar
{
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
break;
}
}
}
-(void)addCustomElements
{
// Initialise our two images
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn1.frame = CGRectMake(0, 430, 80, 50); // Set the frame (size and position) of the button)
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
btn1.backgroundColor = [UIColor yellowColor];
[btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially
In my project I will be using iOS 5.1 and up and no Storyboards or XIBs. Thanks!
Since iOS 5.0, it is no longer a problem to create your own UITabBarController using a line of UIButtons at the bottom of the screen.
In previous versions of the iOS SDK, it was a bit risky as you had to manage the forwarding of the viewWill/viewDidmethods by yourself.
Have a look at the UIViewController Class Reference, section Implementing a Container View Controller, you will find all you need there : UIViewController Class Reference
There is also a featured article explaining exactly what you need : Creating Custom Container View Controllers
Hope this will help,
Related
I am programmatically building a UINavigationContoller for iOS and am having problems making it fully accessible. In loadView I create the main UIView and set it as NOT accessible:
- (void)loadView
{
CGRect viewRect = [[UIScreen mainScreen] applicationFrame];
UIView *tmp = [[UIView alloc] initWithFrame:viewRect];
[tmp setIsAccessibilityElement:NO];
I then add additional UIViews that contain just background images and also set those as not accessible. All views and controls are added onto the "tmp" UIView created above. Here is a "background" view example:
UIImage* microphone = [UIImage imageNamed:#"microphone.jpg"];
UIView* microphoneView = [[[UIView alloc] initWithFrame: CGRectMake(0,0,viewRect.size.width, microphone.size.height)] autorelease];
[microphoneView setBackgroundColor:[UIColor colorWithPatternImage:microphone]];
[microphoneView setIsAccessibilityElement:NO];
[tmp addSubview:microphoneView];
Finally I add a UIButton, UILabel and UIButtonBarItem. I add these last so they are on the top of the view hierarchy. I add accessibility labels and traits to them. Here is the UIButton:
self.recordImage = [UIImage imageNamed: #"record_button.png"];
self.stopRecordImage = [UIImage imageNamed: #"stop_button.png"];
self.recordButton.accessibilityTraits |= UIAccessibilityTraitStartsMediaSession;
self.recordButton = [[UIButton alloc ] initWithFrame: CGRectMake((viewRect.size.width - recordImage.size.width)/2 , (microphone.size.height + (grayBkg.size.height - recordImage.size.height)/2), recordImage.size.width, recordImage.size.height)];
[self.recordButton setIsAccessibilityElement:YES];
[self.recordButton setAccessibilityLabel: #"toggle recording start"];
[self.recordButton setImage: recordImage forState:UIControlStateNormal];
[self.recordButton addTarget: self action:#selector(processButton:) forControlEvents:UIControlEventTouchUpInside];
[tmp addSubview:recordButton];
finally
....
[self setView:tmp];
[tmp release];
I did call UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); when I push this view onto the stack.
With voiceover on, when the view is displayed I can swipe and give each of my elements (the UIButtonBarItem, UILabel, and UIButton) focus and I can activate them with double tap. However, VoiceOver speaks no information about the elements. Testing in the simulator with the Accessibility Inspector shows the labels I have set via aControl.accessibilityLabel = #"the label";
This view is used to record audio. If I activate the buttons and record the audio and stop recording, VoiceOver will now speak the labels for the elements when I focus them? Why is VoiceOver not speaking the information when the view first loads? Any clues appreciated!
I am testing on an iPad 2 with iOS 4.3.3.
If you'd like your view to not be accessible, use:
[microphoneView setUserInteractionEnabled:NO];
This view is being used for audio recording. The problem was that I was setting the AVSession Category to AVAudioSessionCategoryRecord in the viewDidLoad method. This was causing VoiceOver not to speak the view information. I modified the code to set the category to AVAudioSessionCategoryRecord only when the record button is pushed. And I set it to AVAudioSessionCategoryPlayAndRecord when recording is finished. Here is the thread that explains it fully: http://lists.apple.com/archives/accessibility-dev/2011/Jul/msg00002.html
I have an application which has a number of views which are navigated to via a number of custom UIButtons from my main view. The main view is named iBMRViewController and features some welcome graphics in the form of PNG images dropped in through interface builder. It also features 6 custom UIButtons which I have created through code using the following;
// This is the code which creates, and defines the properties of the 'Warning' button on the main view.
UIButton *warningButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
warningButton.frame = CGRectMake(225.0, 270.0, 60.0, 60.0);
[warningButton setTitle:#"" forState:UIControlStateNormal];
warningButton.backgroundColor = [UIColor clearColor];
[warningButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
UIImage *warningButtonImageNormal = [UIImage imageNamed:#"Warning.png"];
UIImage *warningStretchableButtonImageNormal = [warningButtonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[warningButton setBackgroundImage:warningStretchableButtonImageNormal forState:UIControlStateNormal];
UIImage *warningButtonImagePressed = [UIImage imageNamed:#"whiteButton.png"];
UIImage *warningStretchableButtonImagePressed = [warningButtonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[warningButton setBackgroundImage:warningStretchableButtonImagePressed forState:UIControlStateHighlighted];
[warningButton addTarget:self action:#selector(warningButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:warningButton];
Now, this all works fine and the buttons function perfectly, obviously I also have Actions set up for them which work perfectly. On each page I have a UINavigationBar with a UINavigationItem on set up through interface builder and set to take me back to my main view using the following code;
//This is the code which opens up the new view when 'Begin' button is tapped.
-(IBAction)beginHomeButtonAction:(id)sender {
iBMRViewController *controller = [[BeginView alloc] initWithNibName:#"iBMRViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
This also works, however, when it takes me back to the 'iBMRViewController' it ONLY displays what was set up via the interface builder xib file (i.e. the welcome png files). It does not display the buttons I added through code.
Can anyone give me an idea of where I have gone wrong? Would be extremely appreciated.
Thanks
In fact, the beginHomeButtonAction doesn't "take you back to the main view". It creates a new view controller, with a new view, and presents that. It's the same kind of controller class, but not the same instance of that class.
To dismiss the view, and in fact take the user back to the main view, you have to dismiss the view that you presented from the main view. How to do this depends on how you presented then, but you could try popViewControllerAnimated or dismissModalViewControllerAnimated. (google is your friend)
I have a view controller class (child) which extends from view controller class (parent). In the parent class's loadView() method I create a sub-view (named myButtonView) with two buttons (buttons are horizontally laid out in the subview) and add it to the main view. In the subclass I need to shift these two buttons up by 50pixels.
So, I am shifting the buttonView by calling the setFrame method. This makes the buttons shift and render properly but they do not respond to touch events after this. Buttons work properly in the views of Parent class type. In the child class type view also, if I comment out the setFrame() call the buttons work properly.
How can I shift the buttons and still make them respond to touch events?
Any help is appreciated.
Following is snippets of the code.
In the parent class:
- (void)loadView {
// Some code...
CGRect buttonFrameRect = CGRectMake(0,yOffset+1,screenRect.size.width,KButtonViewHeight);
myButtonView = [[UIView alloc]initWithFrame:buttonFrameRect];
myButtonView.backgroundColor = [UIColor clearColor];
[self.view addSubview:myButtonView];
// some code...
CGRect nxtButtonRect = CGRectMake(screenRect.size.width - 110, 5, 100, 40);
myNxtButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myNxtButton setTitle:#"Submit" forState:UIControlStateNormal];
myNxtButton.frame = nxtButtonRect;
myNxtButton.backgroundColor = [UIColor clearColor];
[myNxtButton addTarget:self action:#selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButtonView addSubview:myNxtButton];
CGRect backButtonRect = CGRectMake(10, 5, 100, 40);
myBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myBackButton setTitle:#"Back" forState:UIControlStateNormal];
myBackButton.frame = backButtonRect;
myBackButton.backgroundColor = [UIColor clearColor];
[myBackButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButtonView addSubview:myBackButton];
// Some code...
}
In the child class:
- (void)loadView {
[super loadView];
//Some code ..
CGRect buttonViewRect = myButtonView.frame;
buttonViewRect.origin.y = yOffset; // This is basically original yOffset + 50
[myButtonView setFrame:buttonViewRect];
yOffset += KButtonViewHeight;
// Add some other view below myButtonView ..
}
Button may have overlapped with another view after changing the frame... Just try by setting the background color to the views which are transparent so you can get clear idea which view is overlapping on button.
Make sure your UIButton’s container view can be interacted with. This is the one that gets me. If your UIButton is a subview of another view (not your view controller’s main view), it may not be configured to allow user interaction (the default). Try: containerView.userInteractionEnabled = YES;
Thanks for the help guys, you were right. I forgot to resize the scrollView that I added above the buttons, in the parent view. So, it was overlapping with the shifted buttonView. Changing the background color helped me see it. Thanks for the tip, Chandan.
This should work, my guess and this is just off the top of my head, is that you've got something intercepting your touch events. Basically, there's another view on top of your button.
Good luck.
i need to place menu button in iphone.
i have some touch with android.
In android i display like this by pressing menu.
Is it possible to handle iphone menu button.
if it is not where can i place these.
Thank u in advance.
use the round rect button and change the property into custom place the image as the background image of the button do this for more menu item in your home page.
this is the way i do the menu item.......
I think with Xcode and Interface Builder (iPhone's development platform), getting this task done would be even simpler.
As a brief introduction to iPhone app's UI design, to place this tab bar (as you put it, the menu) on a view, all you need to do is pick up a tab bar from the library and place it on a nib file's view. You can also connect such a tab bar with some codes responsible for internal logic by simply clicking and dragging. It's simple.
btw, a nib file is just some file format Xcode and Interface Builder use to hold the UI data.
iPhones don't have a dedicated slide out menu - everything is on the screen at all times, and there are no purpose built buttons (like back and home).
You should either always show your actions on the screen, or if they are unimportant or infrequent, hide them behind another action button (like a + or wrench icon).
You should use images same as menu items and put custom button over the images.This is the only way to show menu as you want.
Iphone sdk provides tabBar and toolBar, you also can use these in customize forms.This is the correct way to making menu.Iphone design pattern having lot of strengths over android so you can make every thing in Iphone easily but you cant make every thing in android same as iPhone.Iphone having international standards so must use iPhone's controls.
.h
IBOutlet UIScrollView *scrollView;
#property ( nonatomic , retain ) IBOutlet UIScrollView *scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender;
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons;
.m
#synthesize scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender{
NSLog(#"AppleVijayAtFacebookDotCom called");
UIButton *button=(UIButton *)sender;
if (button.tag == 0) {
NSLog(#"hey have clicked first button, this is my tag : %i \n\n",button.tag);
}
else if (button.tag == 1) {
NSLog(#"hey have clicked second button, this is my tag : %i \n\n",button.tag);
}
// ......like this
NSLog(#"button clicked is : %iBut \n\n",button.tag);
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(AppleVijayAtFacebookDotCom:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];//with image
//OR
[button setTitle:[NSString stringWithFormat:#"%iBut",i] forState:UIControlStateNormal];//with title
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 10;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.backgroundColor=[UIColor blackColor].CGColor;
button.layer.borderWidth=2.0f;
button.tag=i;
[self.scrollView addSubview:button];
}
self.scrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
//self.navigationItem.titleView=self.scrollView;//if u have navigationcontroller then enable this line
}
Dont forget to connect the scrollView in interface builder
while creating the scrollview in IB make sure ur scrollView height is 44.which is default to navigation bar.so it will look nice.
in viewDidLoad call
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:30];
OUTPUT
I have a uitabbarcontroller which has one UIViewController added to it. (I reduced to smallest case possible). This viewcontroller calls up a UIView which draws a UIButton via:
- (void)drawRect:(CGRect)rect
{
CGRect brect = CGRectMake(0, 330, 60, 27);
CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:brect];
[button setCenter:centern];
[button setTitle:#"New" forState:UIControlStateNormal];
[self addSubview:button];
}
The button is drawn without the inside text. Only when I touch the display is it then drawn properly.
My solution has been the following: Give the tab-bar controller two UIViewControllers to control. Then force a draw of the second one, and then back to the first one:
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:1];
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0];
This solution works, but it's a stupid hack. Is there a better way to do this?
The code you posted dosen't seem to fit inside a drawRect: implementation. You should add this code to your views init implementation, or to your viewControllers loadView or viewDidLoad implementation.
These two lines do not do anything useful you can leave them out:
CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
[button setCenter:centern];
Whenever you set a frame on a view, the views center is implicitly set to the frames midpoint(center).