iOS: xcode instance of a subview - iphone

I have a view with a button and 8 labels, i want to repeat this view with input from the user to customize these labels, so there will be this base view displayed multiple times on the screen and i don't want them to cover up each other, the button and placement of the labels are the same of each view.
How would i programmatically make a new view appear after user input of the instance view and make sure it doesn't cover up any other view, i hope this isn't to broad, i just want to have one set view with a button and 8 labels, copy it multiple times and display the user input, thanks.

If I'm understanding what you are looking for, you want the display to scroll up showing all the different views that have been created by the user, one after the other.
To accomplish this, you can use a UIScrollView and programmatically add the views to the scroll view as needed. Make sure to increase the contentSize of the scroll view to account for the added views.
Here is some code:
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//create and add as many of yourView as necessary for your project - y would be the the offset so it gets displayed under the other views
YourCustomView *yourCustomView = [[YourCustomView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];
//populate yourCustomView with the appropriate information...
[scrollview addSubview:yourCustomView];
//when you are done adding your views - w and h are whatever your content dictates they are
scrollview.contentSize = CGSizeMake(w, h);
[self.view addSubview:scrollview];
Alternatively, depending on your setup and design, you could use a UITableView with a custom UITableViewCell that displays the pertinent info.
Hope that helps!

Related

Cannot change height of UITableView

In the Apple iPhone Clock App, when a user adds a new alarm a modal view pops up. I am trying to create a UI similar to that.
I currently have a UITableViewController as the root view controller of a UINavigationController.
I also have a UIDatePicker added as a subview to the UINavigationController:
[self.navigationController.view addSubview:mydatePicker];
However, I have about 10+ rows in my UITableview (that of the UITableViewController) and as soon as I added the UIDatePicker I cannot scroll to view all the cells.
I realized that the UITableView size is the same size as it was before I added the UIDatePicker, and therefore I would need to change its size in order to be able to scroll to see all the table cells.
I have tried several things in order to change its size, all to no avail. In the code below I arbitrarily chose 50 for the new height.
First, tried changing the bounds height:
CGRect bounds = [self.tableView bounds];
[self.tableView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
50)];
Then tried to change the frame height:
CGRect tvframe = [self.tableView frame];
[self.tableView setFrame:CGRectMake(tvframe.origin.x,
tvframe.origin.y,
tvframe.size.width,
50)];
Then after googling some more i tried changing the contentSize height:
CGSize thesize = self.tableView.contentSize;
thesize.height = 50;
self.tableView.contentSize = thesize;
None of these appeared to have any effect on the size of the UITableView. I still could not scroll to see all the cells.
I later tried some of the same methods as above but on the UINavigationController instead of the UITableView. I didn't have much luck with this either.
As a last resort, I tried to change the size of the UITableView in the Storyboard editor. I could not figure this out either.
Thanks for any help you can provide.
From the UITableViewController class reference, it:
creates an unconfigured UITableView object with the correct dimensions
and autoresize mask
And a quote from the Table View Programming Guide for iOS which specifically addresses this behavior:
Note: You should use a UIViewController subclass rather than a
subclass of UITableViewController to manage a table view if the view
to be managed is composed of multiple subviews, only one of which is a
table view. The default behavior of the UITableViewController class is
to make the table view fill the screen between the navigation bar and
the tab bar (if either are present).
If you don't want the table view controller setting the size of your tableView, then you need to use a UIViewController instead. See the link that I posted above to the Table View Programming Guide for iOS for other things to consider when going this route.
I had same problem as you mentioned.
I was unable to change the height of tableView of UITableViewController. I tried changing frame and centers of self.view and self.tableview. But failed.
Then I solved it by a trick, changing tableview's contentInset.
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44.0f, 0); // here you can replace 44.0f with your height
You need to make the UITableView a subview of your main view in order to change its size reliably.
The frame is the correct value to alter once you have made this change.
I dont know what is your real Problem . but for change hight of your UITableView and will Display all rows in UITableView write following code.
self.tblView.frame = CGRectMake(0, 0, 320, 50);
please try it.
Your are taking uitableview controller, in place of that use uiviewcontroller in that add the uitabelview. Now you can the the size of uitableview.

dynamically add cells to a grouped uitableview and fit the scroll programmatically

I am trying to create programmatically a uitableview that can scroll and fit based on the number of cells, so if i am adding more cells, i could scroll up and down without the page bouncing.
I tried to do this but it doesnt work:
tblSimpleTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 640) style:UITableViewStyleGrouped ];
tblSimpleTable.scrollEnabled = YES;
tblSimpleTable.dataSource = self;
tblSimpleTable.delegate = self;
[self.view addSubview:tblSimpleTable];
when i add more cells, i can scroll up and down, but the page bounces up so i can't click on the bottom cells. I want to be able to scroll and "stay" where i stop.
UPDATE: just to clarify, since I am using a Grouped table, i dont want to show empty cells, and so i am not sure what should be the height of the able since i may have 1 cell or 100 of them.
Thanks!
The reason you aren't staying where you stop is because your UITableView's height is 640. This means that when you completely scroll all the way down in your UITableView some of the cells will be below the bounds of the view you are looking through. You have two options: decrease your table view height, or add some dummy cells to the bottom of your table view.
Also, if you don't want your tableview scroll to bounce, you could set the "bounces" property of the tableview to NO.
tableView.bounces = NO;

Page Control Question

I am trying to figure out how to add a page control to my view correctly. I have a keypad currently on the view along with a picker, but I want to separate the keypad and picker.
So I want to swipe to page two, and load a different view which essentially just has a picker. Page one will just have a keypad.
I can't find an example of a page control that switches to two separate views. I looked at the apple example PageControl, but that did not solve my issues.
Any help, suggestions are much appreciated.
You're not going to be able to do this with the native keypad - it doesn't belong to any one view, and so cannot be "paged" in the manner you describe.
As for a couple of hints as far as implementing paging: what you'll want to do is make a UIScrollView, set it's contentSize to be two pages wide (or more if you prefer), and set pagingEnabled to YES. Then add your pages as subviews. Note that the UIPageControl does not itself implement paging - it is an indicator only.
Some basic sample code (untested) assuming horizontal paging:
- (void) initPagesForScrollview:(UIScrollView*) scrollView
{
CGRect pageFrame = CGRectMake(0, 0, scrollView.bounds.size.width,
scrollView.bounds.size.height);
scrollView.contentSize = CGSizeMake(pageFrame.size.width * 2,
pageFrame.size.height)
scrollView.pagingEnabled = YES;
UIView* page1 = ...
page1.frame = pageFrame;
[scrollView addSubview:page1];
UIView* page2 = ...
pageFrame.origin.x += pageFrame.size.width;
page2.frame = pageFrame;
[scrollView addSubview:page2];
}
Note the origin of page 2 shifted to the right to make it begin off screen.
You can also do most of this through interface builder, but that's a little more difficult to demonstrate here... Feel free to ask if there's something specific you need.

How To Present Half Screen Modal View?

I have a UIViewController and when a button is pressed, I want a half screen view to slide up with a UIPicker in it.
I made a UIView in IB with the UIPicker along with a UIToolBar with Done/Cancel buttons.
How can I make it so that just this half view slides up and the background view is still showing but dimmed or cant be played with.
I'm using this code so far:
- (void)showModalView
{
[self.popupView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.popupView];
[UIView animateWithDuration:.2 animations:^{
[self.popupView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
Here is a pic: http://www.box.net/shared/static/08ji4s0f6i1b8qubrtz6.png
#Jon:
METHOD-1:
Make your main view transperant by setting its alpha value to 0 and add a subview to the main view which is only half of the main screen and keep it opaque (alpha value as 1) as it would be by default.
Then simply present the view controller using present Modal View Controller.
Keep in mind that because of the transperancy you would be able to see half of the previous view, but wont be able to touch it as there is a transperant view.
METHOD-2:
Another work around is to animate a UIView which is of size half of the existing view.
Then you have to simply follow animation of the UIView.
Here as it is just a UIView that will be added as subview to existing view, you will be able to touch the rest of the screen.
So you can follow either of the methods as per your requirement.
Hope this helps you.
Here is what u need its an open source code on github TDSemiModalView having a half view date picker. Check the demo project inside the code. Here is the link.. Hope it solves your problem.
TDSemiModalClass

Adding views to a scrolling view

I have a view (myContactViewController.xib) which contains a Scroll View (created in Interface Builder, and which takes takes up the entire height of the window and is 960px in width — ie. 3 x the width of a single window). I'm using the Scroll View, with paging, for horizontal navigation of three subviews (all three subviews are 320x460, ie. the entire size of the window). I have created these three subviews via Xcode (command-N), and so for each subview I have a .h, .m and .xib. I have used Interface Builder to build the layout (just some text and a picture for now — but, will eventually contains some buttons) for their views.
Programmatically, I would like the load these three views into my Scroll View, specifying their location within. The .xibs for each of the views I'd like to load into the scroll view are as follow: myContactSub1.xib, myContactSub2.xib and myContactSub3.xib).
I'm wondering if anyone would be willing to explain how to do this?
Thanks,
Kristin.
Sure it's very easy. First set the frame for the subview. The frame specifies where in the superview you want it to appear and is a CGRect. So you might use CGRectMake(0, 0, 320, 460) for the first subview. Then add it to the UIScrollView with addSubview:. Here's an example:
mySubView.frame = CGRectMake(0, 0, 320, 460);
[myScrollView addSubview:mySubView];
This assumes you have a pointer to these views, using IBOutlets. If you need help doing this read up on Xcode Integration
Also take a look at View Geometry