How to build a long interface using interface builder - iphone

I build an UIViewController, that higher than iPhone screen ( the height is 900px ), i build it using UIScrollView.
In the Interface Builder, how to put an View in the bottom part of the UIScrollview ? i've tried using freeform in the inspector -> Simulated Metrics, but every time i move back to the ipad/iphone screensize, the views are mess. And when i ran it in the simulator, the view still a mess

I'd add a view programmatically and make it subview for the scrollview, i don't know how to do it using IB.
If you want to try programmatically can try something like:
UIView*myView=[[UIView alloc ] initWithFrame:CGRectMake(0, 480, 320, 200)];
[myView setBackgroundColor:[UIColor redColor]]; // Just for testing purpose
[scrollView addSubview:myView];

You have to do a bit of it programatically I'm afraid but you can do most of it in a xib.
Your xib would contain a UIView that's your normal view, containing your scrollview.
It would also contain another UIView; this will be as long as you want and will be your scrollview's contents.
In your .h file, have a property like this and attach it to the second UIView in your xib
#property (nonatomic, retain) IBOutlet UIView *scrollingContents;
and in your viewDidLoad, that's where you attach the contents inside the scroll view
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:scrollingContents];
self.scrollView.contentSize = scrollingContents.bounds.size;
}

Related

UIView not appearing on top of UITableView

I have a UIViewController with a UITableView on top of it. I am trying to put a small UIView overlay on top of the table view. I added it both programatically (and used bringSubviewToFront), and then in Interface Builder and it does not appear either way.
Why is that? How can I add the UIView overlay on top of the table view?
EDIT:
I am not using a UITableViewController. It's a UIViewController with a UITableView. Here is the code I used in viewDidLoad:
UIView *joinMediame = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 44)];
joinMediame.backgroundColor = [UIColor orangeColor];
[self.view addSubview:joinMediame];
[self.view bringSubviewToFront:joinMediame];
If you are intending for your view to scroll with the table content, I would make it the tableHeaderView of the table. If you want the view to be static at the top, I would add it as a subview of the controller and then resize the table view frame to make room for the static view.
Note that if the controller is a UITableViewController, self.view is a UITableView, so if you do something like [self.view addSubview:staticView] this won't have the effect you expect.

Scroll Views in storyboarding iphone

I'm trying to set up a scrollView in an app using storyboarding. I have done everything correctly but the scrollview won't scroll in simulator or on iPhone.
MoreViewController.h
#property(nonatomic, retain) IBOutlet UIScrollView *moreScroll;
MoreViewController.m
-(void)viewDidLoad{
[moreScroll setScrollEnabled:YES];
[moreScroll setContentSize:CGSizeMake(1220, 354)];
}
I have connected the scrollView to the files owner, can someone help please
Thanks in Advance
With autolayout is there a new method - (void)viewDidLayoutSubviews
Here is a quick info:
Notifies the view controller that its view just laid out its subviews.
When a view’s bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
You should be fine if you add this:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubViews];
[moreScroll setContentSize:CGSizeMake(1220, 354)];
}
Setting an scroll view’s scroller style sets the style of both the horizontal and vertical scrollers. If the scroll view subsequently creates or is assigned a new horizontal or vertical scroller, they will be assigned the same scroller style that was assigned to the scroll view..
You should be if you add this lines in your code:
This line in .h file
#interface Shout_RouteView : UIViewController<UIScrollViewDelegate>
{
}
#property (retain, nonatomic) IBOutlet UIScrollView *scrollViewMain;
This lines copy in .m file
-(void)viewWillAppear:(BOOL)animated {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
scrollViewMain.frame = CGRectMake(0, 0, 768, 815);
[scrollViewMain setContentSize:CGSizeMake(768, 1040)];
}else {
scrollViewMain.frame = CGRectMake(0, 0, 320, 370);
[scrollViewMain setContentSize:CGSizeMake(320, 510)];
}
}
I suggest that you try to pust the content setting code into the viewWillAppear or viewDidAppear method. Its always best not attempt changing UI features before it is ready for display.

Loading another NIB into one subview of a UIVIEW

I have a simple question that I couldn't see it answered on the whole site,
One XIB file that has a UIView as the main,
in it has another UIView that's connected to one IBoutlet so I can assign it later to load a view from another XIB file. That assignment doesn't work.. it remains blank...
#interface Subclass : UIViewController {
UIView *view1;
}
#property (nonatomic,retain) IBOutlet UIView *view1;
#end
i linked view1 to the subview in the xib file
in implementation in initWithNibName I'm loading Other XIB file and use it's view and assigning it to the view1. Still this doesn't work...
detailControler = [[XibViewControler alloc] initWithNibName:#"XibViewControler" bundle:nil];
//one aproach
[self.view1 addSubview: detailControler.view];
//another aproach
[self setView1:detailControler.view];
If I'm adding the subview programaticaly to [self.view addSubview:detailControler.view] and set a frame to it, it will go fullscreen, and hide the original view.
I also had a different approach that didn't work
How to load multiple views on each button tap when using a UISegmentedVIew
This is how I usually set up a UIView inside another view, although I'm not entirely sure if it's best practice or anything like that:
if(newViewController == nil){
newViewController = [[NewViewController alloc] initWithCoder:nil];
}
newViewController.view.autoresizingMask = UIViewAutoresizingNone;
if([newViewController.view superview] == nil){
[view1 addSubview:newViewController.view];
}
Hope that Helps!

Create a custom UIView as a tableHeaderView programmatically

I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

Programmatically layout iPhone UIView?

I am using the iPhone toolchain on Linux and so I have no Interface Builder. So how could I layout my view in my ViewController subclass? For example, I want a UITextView in the middle of the screen? Should I do this in the loadView or viewDidLoad. Do I also have to set the view for the ViewController subclass to itself?
It is not an easy job to layout all the view using code. Here are some code:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (100, 100, 100, 100)];
[self.view addSubview:textView];
The frame is the place (the first and second argument is x and y coordinator) and the size (the third and fourth argument is width and height of the text view).
Using this way, you can add any view into your class. Some of the view is built in and you don't have to draw yourself, some of them is not, and you need to subclass UIView and override drawRect.
You should do this in viewDidLoad when your main view controller is finished loading
I've written an open source project that does exactly this:
https://github.com/charlesmchen/WeViews
Here's another project that you might find useful:
http://code.google.com/p/layoutmanagers/
I usually build the entire view hierarchy in the loadView method and perform additional setup in the viewDidLoad, for example to set up the subviews content to reflect the data associated to the view controller. The important thing is to set the view controller view outlet in the loadView method.
#synthesize label; // #property(nonatomic,retain) UILabel *label declared in the interface.
-(void)loadView {
// Origin's y is 20 to take the status bar into account, height is 460 for the very same reason.
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0,20,320,460)];
[aView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[aView setAutoresizeSubviews:YES];
// The 150x50 label will appear in the middle of the view.
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake((320-150)/2,(460-50)/250,150,50)];
// Label will maintain the distance from the bottom and right margin upon rotation.
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin];
// Add the label to the view hiearchy.
[self setLabel:aLabel];
[aView addSubview:aLabel];
// Set aView outlet to be the outlet for this view controller. This is critical.
[self setView:aView];
// Cleanup.
[aLabel release];
[aView release];
}
-(void)viewDidLoad {
// Additional and conditional setup.
// labelText is an istance variable that hold the current text for the label. This way, if you
// change the label text at runtime, you will be able to restore its value if the view has been
// unloaded because of a memory warning.
NSString *text = [self labelText];
[label setText:text];
}
-(void)viewDidUnload {
// The superclass implementation will release the view outlet.
[super viewDidUnload];
// Set the label to nil.
[self setLabel:nil];
}
The biggest difficulty is probably understanding how IB settings map to UIView variables and methods, for example the autoresizing mask. Apple's UIView and UIViewController class references are full of useful informations.