Change view with xib - iphone

I have one xib in portrait view and one in landscape view. I am changing xib in rotation like this:
[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:#"%#_landscape", NSStringFromClass([self class])]
owner: self
options: nil];
It's working fine but if I open one view as a subview in the current view then it disappears. For example, on any button click I am adding one view (like a popover) in the current view, but in the rotation subview it disappears. How can I solve this issue?
Any help is appreciated.

You need to make property for that view and when ever you perform rotation you need to re add same view on the top of rest of the views by simply use addSubview function of UIView.

Instead of changing views for different mode,use autoresizing.It will let you work on single view autoResize when rotate.Here is fine example you can learn.
AutoResizing

I think this is not a good approach. You should take one UIViewController with XIB. Now take two views(one for landscape and another for portrait connect it with outlets) outside ViewController's view, and on at rotation time
yourCurrentView = landscapeView/portraitView;
[self.view addSubview:yourCurrentView];
and set it [self.view sendSubviewToBack:yourCurrentView]; //because what ever view added dynamically on self.view remains on top, now your view which was added always show at every mode.
I hope it will help you.

Related

iOS Overlay view which always sits on top?

I haven't been able to find anything on this and feel it likely isn't possible in a simple manner.
Can I have a view, such a loading bar for example which constantly sits over every other view controller until I choose to dismiss it but at the same time any underlying view can still be interacted with? Sort of acting like a system view. Be persistent when presenting new view controllers and all.
Thanks.
Add it as a subview of your window. Like this:
UIView *myView = ...
[self.window addSubview:myView];
Rather than adding it to the window, as #JackyBoy suggests, add it to the window's rootViewController's view. That will rotate along with the device. If you just add it to the window, you may have problems with rotation.
UIView *myView = ...
[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:myView];

Make self.view a UIScrollView?

I'm experimenting with a TabBarController and the default project creates the UITabBarController and also gives you two view controllers.
I want the view of one of these view controllers to be a UIScrollView, i.e. when calling self.view on FirstViewController I want to get back a UIScrollView * and not just a UIView *.
The view controller gets initialised with initWithNibName: but I can't see anything assigning the view property in there.
If this all sounds a bit weird, maybe I'm doing this wrong? I realise I can drop a UIScrollView onto the view that's already created with me, but it just seemed a bit pointless to have a parent view in this case.
Thanks in advance.
Ok, just realised how to do this.
I can do a cast in my code to make UIView a UIScrollView. Like so...
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize = self.view.bounds.size;
Then, in Interface Builder, you can use the inspector to set a custom class for your UIView. I set the class as UIScrollView in here and all seems to work!
If you want to use interface builder. Just load up your nib, delete the view on the left panel, and drag a UIScrollView into the area.
Next link from Files Owner to the new UIScrollView as the view property.
The only downside to doing it this way is in your code, whenever you want specific UIScrollView functions you will have to typecast the view property (using (UIScrollView *)self.view ), or put it in a variable like so
UIScrollView *sview = self.view;
//Then use sview for your changes
The best way would be to do it in code however.

Subviews not drawing in subclassed UIView

I have a subclassed UIView that will not draw it's subviews. Or at least, it's not showing it's subviews. The whole view is loaded from a nib.
Also, strangely, it will not draw the background I set in IB. (I'm using Xcode 4.2).
But it will draw the parent view what I specify in awakeFromNib such as background, border, and corner.
The opaque property is set to NO. Any suggestions are appreciated!
When you load a view from a nib, e.g. using
[[NSBundle mainBundle] loadNibNamed: owner:self]
you still need to then add that loaded view as a subview to one of the views currently visible in your window for that view to become visible. Having an outlet to it from your owner ensures that the view is loaded and you have a reference to it - but that alone does not cause it to be added as a subview to the owner.
So (assuming self is the parent view and the owner in the nib) make sure first that you definitely have the outlet set in the nib from the owner to the view being loaded, and then that you are doing a
[self addSubview:loadedView]
(Where loaded view is the view in the nib who's owner is self).
I think you might get that result if you override drawRect. Try commenting that out and see what happens. If you want to both draw your own content and include subviews, you might be able to do that by calling [super drawRect] at the beginning of your drawRect.

Full screen UIImage view

I have an application with a navigation bar and a tab bar. A user can navigate to a view which displays images in a scroll view. I'd like to have the same behavior as the iPhone photo app: Nav bar at the top, tool bar at the bottom, which will hide or show based upon a tap.
I'm moving my view to the window object in order to achieve full screen mode. This works fine:
myView = [self.view retain];
self.view = nil;
[window addSubview:myView];
But when I want to redisplay the Nav & tool bar, I run into a problem. The bars show fine, but the view is empty, and I can't seem to add any content to the view:
[myView removeFromSuperview];
self.view = myView;
I got a lot of good info from this post
but can't quite get the right combination.
By simply setting the controller's view, you aren't adding it as a subview to anything else, so it will never appear.
Moving views around like this can get a little tricky. I recommend that you not move the view from one to the other, but instead have two UIViews. Add second UIView to the window's subview and set it to hidden=YES initially. When you want to show it, set the image for the UIImageView, and then set the hidden property to NO.
what's wrong with just using setNavigationBarHidden: animated: and setToolbarHidden:animated:?

Multiple Views in one Window

I'm writing a view based app, but I'm a bit confused about loading my views. I'd like to have four different views loaded at the same time in the same window. I can't seem to figure out how to do this. I'd prefer to do everything programatically rather than with the interface builder if possible.
My 4 views are: a UIView, a UIWebView, a UITableView and another UIView with buttons.
Thanks in advance for the help.
Views in an iPhone app are arranged hierarchically - that is, each view has a "parent" view (excepting the root view). The interesting bit here is that UIWindow is itself a subclass of UIView, so you can add all four views to your window directly. (This may not be the best approach, but it's perhaps the simplest.)
All you really have to do is initialize each of your four views programmatically with the location and dimensions you want them to have in the UIWindow. You do this by giving each view a frame parameter, either in the init method or afterwards (depending on the type of view). So, for example, in your app delegate you could add this code:
CGRect frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
[window addSubview:view];
This will create a 100x100-pixel view and add it to the upper left corner of the window. You can do similar things for each of the other three views.
Note that developers usually don't initialize views directly in the app delegate - a better approach might be to have a fifth view take the place as the root view for the other four, then add that root view to the window. You can use a view controller for the fifth view to make this task easier - move the view initialization code into that view controller's implementation, then from the app delegate you can just instantiate the view controller and let it take over from there.
You can use [self parentViewController] to get access to the parent UIView