How To Customise the Tab Bar (UITabBar) in an iPhone Application (Part 2 of 2)
2012-10-19 15:58
991 查看
This is the much overdue followup to Part 1 which can be found here:
http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/
We had subclassed UITabBarController and hidden the existing buttons. Now all we need to do is replace then and create the functionality.
To download a demo project demoing the below click here: https://github.com/rumex/RXCustomTabBar
So, back to XCode.
Let’s create a new method called addCustomElements.
First we need to declare our four new UIButtons:
And declare our new method:
So now our CustomTabBar.h looks like this:
In our CustomTabBar.m we are going to create our addCustomElements method.
Now we need to call this method in viewDidAppear. Our viewDidAppear should now look like this:
We are now done. If we run up our application we will see our new custom buttons at the bottom of the screen. Unfortunately they still don’t do anything so lets now build in the functionality to allow the buttons the change tab.
In our CustomTabBar.h let’s declare it.
Now in our CustomTabBar.m we need to add it. We are going to do three things.
We are going to use a switch command to see which btnID was passed.
We are going to set the selected state of each button according to which btnID was passed.
Finally we will change the selectedIndex parameter or out TabBarController which will actually change our tab.
So the code will look like this:
So now we have a method that will change the state of the buttons and the selected page depending on which btnID is passed to it.
Finally we need to create the buttonClicked method that we set to be called on the TouchUpInside event of our custom buttons.
All it will do is get the tag number of the button and then call our selectTab method passing the tag number as a parameter.
And that should be all there is to it. If you run this up you will now have a working custom tab bar.
NOTE:
The only functionality this doesn’t reproduce is if you press the built in tab bar button of the currently selected tab it will pop any navigation controller back to the root (if there is one on the page). To replicate this on our fake tab bar we could replace the line there we set the selectedIndex of the tab controller with this code:
This checks to see if the tab of the button you are pressing is currently selected. If it is then it will pop the navController on that page back to root, alternatively it will just select the tab. But be careful, this is not a complete solution. This assumes that all of your tab has a navigation controller. If you put this on a page that didn’t and then you press the tab bar button twice it would crash out so if some of your pages have a controller and some don’t that you either want to code in a test to see if one is present, or you could simply hard code additional conditions to the if clause which checks which tab is active and only allow the navcontroller code to run for tabs you know have them.
Here you can download a project based on this method: https://github.com/rumex/RXCustomTabBar
http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/
We had subclassed UITabBarController and hidden the existing buttons. Now all we need to do is replace then and create the functionality.
To download a demo project demoing the below click here: https://github.com/rumex/RXCustomTabBar
3. Add My Own Items
Now we are going to create our new buttons. You need to create your four buttons in both a selected and unselected state and add them to your project. Assuming a portrait orientation you are looking at 320px wide in total and 50px high. I am going to assume that our new buttons are 80px X 50px each.So, back to XCode.
Let’s create a new method called addCustomElements.
First we need to declare our four new UIButtons:
4. Making it all Work
Let’s make a new method called selectTab that take an integer parameter called tabID to tell it which tab was selected.In our CustomTabBar.h let’s declare it.
We are going to use a switch command to see which btnID was passed.
We are going to set the selected state of each button according to which btnID was passed.
Finally we will change the selectedIndex parameter or out TabBarController which will actually change our tab.
So the code will look like this:
Finally we need to create the buttonClicked method that we set to be called on the TouchUpInside event of our custom buttons.
All it will do is get the tag number of the button and then call our selectTab method passing the tag number as a parameter.
NOTE:
The only functionality this doesn’t reproduce is if you press the built in tab bar button of the currently selected tab it will pop any navigation controller back to the root (if there is one on the page). To replicate this on our fake tab bar we could replace the line there we set the selectedIndex of the tab controller with this code:
Here you can download a project based on this method: https://github.com/rumex/RXCustomTabBar
相关文章推荐
- How To Customise the Tab Bar (UITabBar) in an iPhone Application (Part 1 of 2)
- uiview 某点返回 色值 How to get the color of a pixel in an UIView?
- How to get IOleSite interface of the WebBrowser in an ActiveX control
- How do I lock the orientation to portrait mode in a iPhone Web Application?
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- How to customise the TWebBrowser user interface (part 1 of 6)
- How to change the text color of the selected property sheet tab in Visual C++
- How to customise the TWebBrowser user interface (part 2 of 6)
- how to change the background of actionbar.tab
- How to customise the TWebBrowser user interface (part 3 of 6)
- How to customise the TWebBrowser user interface (part 4 of 6)
- How to fix Rosetta stone error 2123 -- "This was an error in the application"
- How to customise the TWebBrowser user interface (part 5 of 6)
- How to fix Rosetta stone error 2123 -- "This was an error in the application"
- How to customise the TWebBrowser user interface (part 6 of 6)
- How to access the properties of an object in Javascript
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- How to make an SDL window the child of another window in Win32
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."