开发者

How to initialize UIView from separate file in ViewController?

I've always used IB but am trying to do everything through code and I'm failing at this task.

I have a ViewController to handle User Inputs and 2 UIViews which will both be visible at the same time(e开发者_StackOverflow社区ach in a separate header/implementation UIView file):

1 UIView represents a custom tab bar that changes (bottom 50 px) 1 UIView represents the displayed interface (everything above the tab bar)

Each needs to exist within its own frame, initialized from the ViewController so it can control them and what they display.


Bra, UIViewControllers have only one UIView as part of their guts.

That is, "view" ... i.e. ... the actual property view, as in self.view = something or view.hidden = YES.

However you can, of course, add as many subviews as you like to that view.

This is how views are used normally. Almost every .view has subviews inside it.

UIView *bottomThing = [[UIView alloc] init];
bottomThing.frame = CGRectMake whatever
UIView *otherThing = [[UIView alloc] init];
otherThing.frame = CGRectMake whatever

[view addSubview:bottomThing];
[view addSubview:otherThing];

In the example, we added two subviews to our main "built-in" view, which you refer to as simply "view". So we added bottomView to our "view" and we added topView to our "view."

The subviews you add could be either plain old UIView, or, your own special subclass of UIView.

MySpecialView *bottomThing = [[UIView alloc] init];
bottomThing.frame = CGRectMake whatever
ExtraordinaryView *otherThing = [[UIView alloc] init];
otherThing.frame = CGRectMake whatever

[view addSubview:bottomThing];
[view addSubview:otherThing];

(I guess FTR conceivably you could subclass UIViewController to have more than one view inside it, but that's completely pointless and irrelevant to this question.)

From your UIViewController you can manipulate the subviews in any way you want.

For example [bottomThing doStuff:3.7], bottomThing.hidden=YES, etc etc.

Once again it is absolutely normal to add more subviews inside your main "view" - this is the basic way in which iPhone apps are made. There is only one ".view" - you add more views inside it as you wish. Hope It Helps.


UIView *myView = [[UIView alloc] init];
[self.view addSubview:myView];
[myView release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜