I want to create a scrollView having many sub views as shown in image.
All views having a labels & image within it.
Number of views add in scrollView are dynamic.
And data of that views is also dynamic.
So that I can't make a static view in program and use it for display.
I want to make scrollview's subview like TableView with custom cells.
Like make a object of that TableViewCell and use it.
Can I use ViewController for that?
If i understood you question true, you need something like dynamic content of scrollView. So you need an array to control how many cell you will put into scrollView and create label, imageView or whatever you need.
For example like that;
//You will need to clean your scrollView Content in everytime
for(UIView *view in [yourScrollView subviews]){
[view removeFromSuperview];
}
for(int i=0;i!=[yourArray count];i++)
{
labels[i]=[[UILabel alloc]init];
//anyInteger is about your views place.
views[i]=[[UIView alloc]initWithFrame:CGRectMake(21, i*anyInteger, 300, 50)];
views[i].backgroundColor=[UIColor colorWithRed:0.1 green:0.2 blue:1.8 alpha:0.0];
[views[i] addSubview:labels[i]];
[yourScrollView addSubview:views[i]];
}
These codes will help you about insert objects in yourScrollView. I didnt test this yet but i guess it will give you an idea.
Related
I am having more lines of contents which exceeds the ViewController height, I want to show it in same screen by scrolling. Is there any way to achieve that?
Use a UIScrollView and put your contents in it.
You can use UIScrollView or UITableView for the same.
Add all views as subviews of one super view say containerView. Add containerView as sub-view of UIScrollView. Do not forget set appropriate content size of UIScrollViiew. You can do it by proper autolayout or programatically as
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
You can also use UITableViewController with static cells. You can add view in static cells and you are good to go. UITableViewController itself manages scrolling.
Hope it helps. Happy Coding!!
You can use a UIScrollView (As #luckyShubhra and #chedabo suggested) , you can setup the UI correctly in Storyboard in this case and have the contentsize of the UIScrollView set to the size of the viewController contained in the UIScrollView...
Then also the UITableView (As #luckyShubhra suggested)... if your data is setup to deal with as a list of NSStrings or object, this can work well...
Then if the content is a NSString, and you would use a UILabel, I would suggest changing it to a UITextView... This will make it possible to scroll as well. If the contents is not to be edited just change the Selectable and Editable booleans to false...
UIScrollView or even UITextView can help that too. if it is HTML, you may consider of rendering it with UIWebView.
just make sure the control height is lesser than the view controller, then it will render the contents in a scrollable manner.
I want to create an ios app, using objectiveC, which has a scrollable screen which displays several collection views one below the other.
I am uploading a screenshot of a similar screen from a popular ecommerce app.
I tried creating a tableview in such a way that all the rows are of 0 height but with custom headers. I am displaying different view controllers in different headers. I am able to parse the table properly but when I scroll down, it doesn't scroll smoothly.
Can you help me with this. Thanks in advance!
you can create a vertical flow collection view representing your each category, then for each collection view cell , return a horizontal flow layout collection view. This will do the trick.
Use a collection view within a collection view. The outer collection view will have vertical scrolling, while the inner will have horizontal scrolling. This can be done with the scrollDirection property on the flow layout.
Just create a scroll view and give the code like this...
-(void)scrollView
{
NSLog(#"scrollView");
for(int i=0;i<arrImages.count;i++)
{
CGRect frame;
frame.origin.x=self.scrollViewTop.frame.size.width *i;
frame.origin.y=0;
frame.size=self.scrollViewTop.frame.size;
scrollViewTop.scrollsToTop=NO;
UIImageView *image=[[UIImageView alloc]initWithFrame:frame];
[self.scrollViewTop addSubview:image];
[scrollView addSubview:lblScrollView];
UILabel *lbl=[[UILabel alloc]initWithFrame:frame];
[scrollViewTop addSubview:lbl];
}
self.scrollViewTop.contentSize=CGSizeMake(width, height);
}
what ever you want inside the scroll view just add it as a sub view..
I am trying, programmatically, to add subviews to my view. The process goes as follows:
1) Fetch an array of medias from the web
2) If it is a picture, build a UIImageView and add it to the bottom of the view. If it is text, build a UITextView and add it to the bottom of the view
3) Repeat step 2 until the end of the array
The problems:
How do I create the views if I don't know what will be the height of them? It seems that to add a view I need to specify its frame.
How do I add each view to the bottom of the already existing views?
Thanks in advance,
Tiago
If you don't know the size of the view, call the sizeToFit and layoutIfNeeded method after adding the view. Here's the code:
Objective-C :
[superView addSubview: view];
[view sizeToFit];
[view layoutIfNeeded];
Swift :
superView.addSubview(view)
view.sizeToFit()
view.layoutIfNeeded()
You can always create the view with a zero-sized frame (CGRectZero) and set its frame later once you know it.
I think you can use a UITableView to implement this feature. The tableView is normally used for displaying 'N' number of items. You can create two custom cells,
1 for displaying an Image
2nd for displaying a textview.
And then based on the item type of the Array you can reuse these custom cells.
I'm assuming you are looking to initialize and then add the view as a subview. If so, you can do the following:
UIView *yourView = [[UIView alloc] initWithFrame: CGRectZero];
//Note - CGRectZero is equivalent to CGRectMake(0,0,0,0)
[superView addSubview: yourView];
Now when you are ready to resize the view, you can do so as follows:
[yourView setFrame: CGRectMake(newXOrigin, newYOrigin, newWidth, newHeight)];
or alternatively
[yourView sizeToFit];
So I've figured an answer to both my questions:
How do I create the views if I don't know what will be the height of them?
After getting the content from web, call .layoutIfNeeded() to lay out the view. You are now able to use the contentSize of the view to draw it. See example below.
How do I add each view to the bottom of the already existing views?
2 ways:
1) Use a UITableView, easier and more reliable
2) Have a variable to hold the bottom of the stack of views, and update it every time you add a new view. Use this variable to decide where to add a new view.
Here's the code with 2):
var yAxis: CGFloat = 0
var textContent: String = "" {
didSet{
var textView = UITextView()
textView.text = textContent
textView.layoutIfNeeded()
textView.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width, textView.contentSize.height)
textView.frame.origin.y = yAxis
yAxis += textView.contentSize.height
view.addSubview(textView)
}
}
Thanks to #Dhruv Ramani for the hint.
I need to get an array of all the subviews in a UIScrollView. Right now I'm using
NSArray *subviews = [myScrollView subviews];
but this seems to only be returning the subviews that are visible at the time the code is run. I need all the subviews in the whole extent of the UIScrollView, even those that are currently hidden (as in off screen). How would I get that?
Essentially, I'm looking for something like the contentSize property of a UIScrollView, except instead of returning just the size of the UIScrollView if it were big enough to display all of it's content, I want it to return the content itself.
EDIT: I think I've figured it out: the scroll view this isn't working for is actually a UITableView - and I think it's deque-ing the cells that are off screen on me, and that's why they aren't showing up. I'm going to do some testing to confirm.
Try with following code its working for me.
for(UIView * subView in myScrollView.subviews ) // here write Name of you ScrollView.
{
// Here You can Get all subViews of your myScrollView.
// But For Check subview is specific UIClass such like label, button, textFiled etc.. write following code (here checking for example UILabel class).
if([subView isKindOfClass:[UILabel class]]) // Check is SubView Class Is UILabel class?
{
// You can write code here for your UILabel;
}
}
tl;dr
It turns out that
NSArray *subviews = [myScrollView subviews];
will indeed return all the subviews in a UIScrollView *myScrollView, even if they are off-screen.
The Details
The problem I was actually having was that the scroll view I was trying to use this on was actually a UITableView, and when a UITableViewCell in a UITableView goes off-screen, it actually gets removed from the UITableView - so by the time I was calling subviews, the cells I was looking for were no longer in the scroll view.
My workaround was to build all of my UITableViewCells in a separate method called by my viewDidLoad, then put all of those cells into an array. Then, instead of using subviews, I just used that array. Of course, doing it this way hurts the performance a little (in cellForRowAtIndexPath you just return the cell from the array, which is slower than the dequeueReusableCellWithIdentifier method that is typically used), but it was the only way I could find to get the behavior I needed.
I wrote a code with a UILabel with the name of a meteorological station. Later, I added the code to put a UITableView with a grid view like this website explains http://www.recursiveawesome.com/blog/2011/04/06/creating-a-grid-view-in-ios/
The problem is that now the Table view is shown in all screen and the label can't be seen.
How do I make to put the elements in this order?
UILabel
UITableView
Thanks!
1 With a nib:
If you use a nib you can simply size / layout your label and table view so that they are positioned as desired. A UITableView can be made any size and take up any portion of the screen.
2 Without a nib
Create / alloc / initialize your label and table, and then add them to the view:
[self.view addSubview:myTableView];
[self.view addSubview:myLabel];
The magic step to this is that you need to set the frame of both your label and table view. Thats something that is really custom so I cant help you with that without more direction however it may look something like this:
// the number 10 is used for padding purposes
CGSize labelWidth = CGSizeMake(self.view.frame.size.width-20, 1000.0f);
CGSize textSize = [myLable.text sizeWithFont:myLable.font constrainedToSize:labelWidth lineBreakMode:UILineBreakModeWordWrap];
myLable.frame = CGRectMake(10, 10, labelWidth.width, textSize.height);
myTableView.frame = CGRectMake(0, myLable.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height - (myLable.frame.origin.y+10));
Please note that calculating frames can be done MANY ways. Also you will probably have to recalculate the frames manually for rotations.
Hope this helps Good Luck!
Depending on the answer to my questions in the comments, there are several answers.
If you want the label to always be on top whether or not the tableview is scrolled, make sure you are using an instance of UIViewController, then create your two views. Set the frame appropriately and then add the label view and the table view as subviews to the main view.
If you want the label to scroll away, that's even easier. Your UIViewController can remain a subclass of UITableViewController. UITableView has a property called tableHeaderView. myTableView.tableHeaderView = labelView;
You could to like Rachel said
[self.view addSubview:myTableView];
[self.view addSubview:myLabel];
or after placing it
[self.view bringSubviewToFront:myTableView]