From http://blog.zhaojie.me/2010/12/iphone-composition-resistant-uitabbarcontroller.html
I am writing an iPhone application recently.ProgramBased on monotouch, the development issues are basically built on the interface elements. This program interface is relatively complex, so I combined it based on my own ideas. It turns out that uitabbarcontroller cannot be placed in other views, instead, it can only be directly placed on the window (or in the uinavigationcontroller in the window). Otherwise, the interface will be shifted down. Although there is workaround, you can only sigh deeply when the uitabbarcontroller rejects the combination.
Let's take a look at the final effect of this interface:
Here we will briefly describe the effect. After opening the program, you will see a standard interface with two labels. Currently, the first interface is selected with a button. Click the button to switch the entire interface (along with the tab bar below) to the new interface and return it. There is another button in the second tag, and the button will be switched to the new level. Note that the tab bar below will remain unchanged at this time. However, the most important thing about this program is that there is a text area on the tab bar, which is independent of each view in the tab bar.
Obviously, a uinavigationcontroller is needed as the root element, and put it in a uitabbarcontroller and text area. In the second tag of uitabbarcontroller, place it in another uinavigationcontroller. Because the uitabbarcontroller and text area need to be displayed and disappear at the same time during the "root navigation" switchover, I naturally intend to combine uitabbarcontroller and a uilabel into a custom uiviewcontroller. This interface should be very easy, because the combination of controls is a common method for writing interfaces.
So I created a customcontroller. XIB file, put a uitabbarcontroller and a uilabel, and added some operation logic (display the coordinates when dragging ). The logic for text display is customcontroller. In actual applications, it will also interact with the Views in uitabbarcontroller. Therefore, I will hide these logics in customcontroller. I think this combination method is very reasonable. However, a strange situation occurs after mytabbarcontroller is displayed on the interface:
Of course, the above is only the case after I put the view of uitabbarcontroller into the uiview control in the window: the overall downward offset. This offset is the height of the top status bar, which makes me confused. After an afternoon's tangle experiment, I concluded that uitabbarcontroller seems to resist the combination. Specifically, uitabbarcontroller positions the uitabbar control in the view at the bottom of the screen. However, when calculating the position, it does not focus on its parent container, rather, I thought that I must be on the root form, so I would consider the height of the top status bar. Therefore, if its parent view is not counted from the top of the interface (including the status bar), the location of the uitabbar will be offset.
A senior IOS developer told me that it is not the standard usage of uitabbarcontroller to combine uitabbarcontroller into another uiviewcontroller. If I want to combine them, I should use the uitabbar control to work with them and process the switching logic by myself. However, I always think that the UI elements (not just controls) should be aware that they will participate in the combination. For example, uitableviewcontroller and uiimagepickercontroller, which are used in combination are their natural responsibilities. In my opinion, for example, other (I used) UI libraries, as long as you set the dock for each control (similar to the auto Resizing in IOS), the combination should be random and natural.
Since they cannot be combined, it seems that the extension method only inherits-so that I cannot use interface builder to draw the interface, which is a lot of trouble. Here I create a mytabbarcontroller that inherits uitabbarcontroller and adds some logic:
Public class Mytabbarcontroller : Uitabbarcontroller { /* Constructors */ Public override void Viewdidload (){ Base . Viewdidload (); This . M_label = New Uilabel () {Text ="Hello monotouch" , Textalignment = Uitextalignment . Center, frame = New System. Drawing. Rectanglef (0,400,320, 20), autoresizingmask = Uiviewautoresizing . Flexibletopmargin }; This . View. addsubview ( This . M_label ); This . View. bringsubviewtofront ( This . M_label );} Private Uilabel M_label; Public override void Touchesmoved ( Nsset Touches, Uievent EVT ){Base . Touchesmoved (touches, EVT ); VaR Touch = ( Uitouch ) Touches. anyobject; VaR Location = touch. locationinview ( This . View ); This . M_label.text = location. tostring ();}}
In this way, it is almost the same. The rest is the simple nesting relationship, and the navigation bar of "root navigation" is hidden when you switch to the second tab.
If you have a better practice, please let me know.