I have scroll view with label and a table view and a button . I just want to scroll the scrollview and the table view must display all the contents but tableview must not scroll. Below the tableview i have a button. How to set the frame of the button so it comes exactly below the table?
Thanks
Maybe you would like to set the YourTableView.userInteractionEnabled = NO?
Yes we can disable the scrolling the tableview.
Goto->xib->select table->Goto 1st tab->unselect the scrolling Enabled.
The answer for your Second Question.
Put the UiView in footer of your table and then place the button in that UIView you want to show in bottom.
It will always show at the bottom.
If you want to place button programmatically use following code in viewDidload method.
///--------Table Footer is Set here
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIButton *adddays = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 260, 44)];
[adddays setBackgroundImage:[UIImage imageNamed:#"abcd.png"] forState:UIControlStateNormal];
[adddays addTarget:self action:#selector(buttonaction) forControlEvents:UIControlEventTouchDown];
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(75, 12, 250, 20)];
[text setBackgroundColor:CLEAR_COLOR];
[text setText:#"Title for your button"];
[text setTextColor:XDARK_BLUE];
text.font=[UIFont fontWithName:#"Arial-BoldMT" size:18.0f];
[footer addSubview:adddays];
[footer addSubview:text];
[table setTableFooterView:footer];
This is assuming you have created IBOutlets for your scrollView, tableView and button, and hooked them up appropriately.
I find it useful to remember that we're only messing with the y-values of a CGRect (origin.y & size.height) - The x-values should be set up in the xib.
I've commented this profusely to illustrate my point better, usually I would only comment where appropriate
-(void)viewDidLoad {
[self.tableView setScrollEnabled:NO];
// Get the number of rows in your table, I use the method
// 'tableView:numberOfRowsInSection:' because I only have one section.
int numOfRows = [self.tableView numberOfRowsInSection:0];
// Get the height of your rows. You can use the magic
// number 46 (44 without including the separator
// between rows) for the height of your rows, but because
// I was using a custom cell, I had to declare an instance
// of that cell and exctract the height from
// cell.frame.size.height (adding +2 to compensate for
// the separator). But for the purpose of this demonstration
// I'm going to stick with a magic number
int rowHeight = 46; //Eww, Magic numbers! :/
// Get a reference to the tableViews frame, and set the height
// of this frame to be the sum of all your rows
CGRect frame = self.tableView.frame;
frame.size.height = numOfRows * rowHeight;
// Now we have a frame with the exact size of our table,
// so set the 'tableView.frame' AND the 'tableView.contentSize'
// to that. (Because we want ALL rows visible as you
// disabled scrolling for the 'tableView')
self.tableView.frame = frame;
self.tableView.contentSize = frame.size;
// Now we want to set up the button beneath the table.
// We still have the 'frame' variable, which gives us
// the tableView's Y-origin and height. We just add these
// two together (with +20 for padding) to get the origin of the button
CGRect buttonFrame = self.button.frame;
buttonFrame.origin.y = frame.origin.y + frame.size.height + 20;
self.button.frame = buttonFrame;
// Finally, we want the `scrollView`'s `contentSize` to
// encompass this entire setup (+20 for padding again)
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height = buttonFrame.origin.y + buttonFrame.size.height = 20;
self.scrollView.contentSize = scrollFrame.size;
}
You could stop the scrolling the table view. But you shouldn't be adding a tableview inside a scrollview. UITableView is subclass of UIScrollView and adding one scrollView on another will create problem. I suggest you to remove the scrollview and use the tableview alone ( as the tableview itself is a scrollview).
Related
I have a UIScrollView which is 208pt wide and 280pt tall that contains custom buttons that are 200pt wide and 280 pt tall with 8pt gaps between them. This scrollview has paging enabled but doesn't clip the subviews so that it always snaps to having one button centered but shows the other ones that go off screen. I am trying to make the field in which you can swipe through the buttons take up the full width of the screen, and I am trying to accomplish this with a secondary custom subclass of UIScrollView called PagingView which just has a UIScrollView property and passes all hits on it down to its scrollview. For whatever reason, though, when I try it without the paging view like this:
unsigned height = self.view.frame.size.height;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60, height - 308, 208, 280)];
scrollView.pagingEnabled = true;
scrollView.clipsToBounds = false;
scrollView.showsHorizontalScrollIndicator = false;
[self.view addSubview:scrollView];
It works, albeit with the field I can interact with the scrollview limited to its frame. However, when I try it with the scrollview:
unsigned height = self.view.frame.size.height;
pagingView = [[PagingView alloc] initWithFrame:CGRectMake(0, height - 308, 320, 280)];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60, height - 308, 208, 280)];
scrollView.pagingEnabled = true;
scrollView.clipsToBounds = false;
scrollView.showsHorizontalScrollIndicator = false;
pagingView.scrollView = scrollView;
[self.view addSubview:pagingView];
[self.view addSubview:scrollView];
It works, but I am able to swipe anywhere on the screen to move through the scrollview. How do I remedy this?
Not clear what you are doing here. You are adding two scroll views to self.view but actually you need only one.
The easiest way with least code is to use one plain UIScrollView, present your buttons as subviews of correctly sized UIViews (you need the gaps around the buttons), and enable paging for the scroll view. Done.
Please note that the measurements you describe do not work out. The scroll view width is 208, the button 200, so the wrapper view should have width 208 and the origin.x of the button should be 4. The scroll view height is 280, the button as well, so there is no vertical margin: wrapper height also 280, button origin.y is 0.
I have a UITextView added on my UIView programmatically. The textview added is not editable, it is just to display some data. The data displayed in the textview is getting from array.The no of the textviews are same as texts which I Stored in Array.Textview is displaying from start to end one by one.I need to set the postion of textview one by one means first textview come and set at some postion,second will be set as just before first textview,third will be just before on second and so on.. Note: Something like using Animation. suppose one textview come from top and it stops at Y postion at 250 then next textview will set at just before that..it cant come upto 250 of Y postion. and so on. I have no clue how to do this. Please give me some ideas.
Don't Consider the array and the UITextView data.... its fot temporary..
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:#"One",#"Two",#"three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Ten", nil];
for(int i=0;i<[array count];i++) {
UITextView *textView = [[UITextView alloc]init];
[textView setFrame:CGRectMake(20, 20*i+height, 200, height)];
[textView setBackgroundColor:[UIColor redColor]];
[textView setText:[array objectAtIndex:i]];
[self.view addSubview:textView];
}
you want to add textviews programmatically in your view based on the array,if you are adding textviews to your view then there is a problem(if u are having more number of textviews then you must take scroll view ) that's why better to take scroll view and add scroll view to your view after that you will add textviews to your scroll view ,
now take constant x-coordinate value ,width and height only change y-coordinate value and you must give the content size to scrollview .
for(int i=1;i<[array length];i++){
text_desc = [[UITextView alloc] initWithFrame:CGRectMake(63,64*i,370,60)];
text_desc.font = [UIFont fontWithName:#"Helvetica" size:13.0];
text_desc.editable=NO;
text_desc.tag=1;
}
and then set srollview content size,
scroll.contentSize = CGSizeMake(0,total*87);
//loop over the substrings of type Array to add textfields at run time
for (int i = 0; i < [substrings count]; i++)
{
CGRect frame = CGrectMake(0, i * 40, 100, 30);
UITextField * txtDynamic = [[UITextField alloc] initWithFrame: frame];
txtDynamic.text = [substrings objectAtIndex:i];
//add as subview
[view addSubview:txtDynamic];
//if you are not using ARC release the txtDynamic
}
Note
If number of UITextField more i.e it goes out of screen, then Add UITextField to UIScrollView and you can make its Size and content size dynamic.
Hope this will give you some clue.
hey mate then take one scrollview and in it just set all textview with its ContentSize like bellow
here i give an example ,
Note: this is just example here take one UIScrollView and give name scrview and after add your all this UITextField in it.
you add this code after you add data in textfield
txt1.frame = CGRectMake(txt1.frame.origin.x, txt1.frame.origin.y, txt1.frame.size.width, txt1.contentSize.height);
float txtscreen1 = txt1.frame.origin.y + txt1.contentSize.height + 10;
txt2.frame = CGRectMake(txt2.frame.origin.x, txtscreen1, txt2.frame.size.width, txt2.contentSize.height);
float txtscreen2 = txt2.frame.origin.y + txt2.contentSize.height + 10;
txt3.frame = CGRectMake(txt3.frame.origin.x, txtscreen2, txt3.frame.size.width, txt3.contentSize.height);
//.....and so on to 10
float txtscreen10 = txt3.frame.origin.y + txt3.contentSize.height + 10;
txt10.frame = CGRectMake(txt10.frame.origin.x, txtscreen10, txt10.frame.size.width, txt10.contentSize.height);
float scrollviewscreen = txtscreen10.frame.origin.y + txtscreen10.frame.size.height + 20;
scrview.contentSize=CGSizeMake(320, scrollviewscreen);//take scrollview
i hope this help you....
:)
Is there any way out .., that I can create a UITableView in a view such that it doesn cover up entire the screen...
For example :
I want the first half of the screen to be a UITextView and the next half portion of my screen to be
a UITableView..
Thanks in advance
CGRect cgRct = CGRectMake(0, 50, 320, 250);
UITableView *tableView1 = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
//tableView.editing = YES;
tableView1.dataSource = self;
tableView1.delegate = self;
tableView1.backgroundColor=[UIColor clearColor];
[self.view addSubview:tableView1];
You are probably creating a UITableViewController for your tableView and then realizing that you can't change the size of the tableView. Am I right? If that is the case then don't create a UITableViewController, just create a normal UIViewController and than add your tableView using Interface Builder or the code which other people posted here.
In Interface Builder (if you are using it), just drag-drop a UITableView to your UIView and adjust its width, height and y position in the Inspector.
If you are programmatically creating the view, change the frame of the UITableView
CGRect tablefrm = [tableViewObject frame];
tablefrm.origin.y = 240.0f //new origin
tablefrm.size.height = 240.0f; //new height
[tableViewObject setFrame:tablefrm]; //assuming already added the tableview to the view.
I am trying to create a new button of a specific size in a UIView. As it happens, this view will be used in a tableViewCell, like this.
UIImageCtrlBtnView *newView = [self initImageCtrlBtnsInViewWithHeight: 50 withWidth : 280];
[cell.contentView addSubview:newView];
I create a new view and a bunch of buttons in the method initImageCtrlBtnsInViewWithHeight.
-(UIView*) initImageCtrlBtnsInViewWithHeight: (CGFloat) rowHeight withWidth: (CGFloat) rowWidth {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rowWidth, rowHeight)];
newView.tag = kImageCtrlBtnViewTag;
newView.clipsToBounds = YES;
// finding the right size of the button by setting the button size to some fraction
// of the view size.
CGRect largeButtonFrame = CGRectMake(rowWidth*0.1+newView.frame.origin.x, rowHeight*0.2+newView.frame.origin.y, rowWidth*0.8, rowHeight*0.6);
UIButton *largeMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
largeMoreButton.tag = kLargeMoreBtnTag;
[largeMoreButton setTitle:#"Add Photo" forState:UIControlStateNormal ];
[largeMoreButton setFrame: largeButtonFrame];
// I check the size of the button frame.
DLog(#"large more btn frame: %d, %d, %d, %d", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);
etc.
The problem is that when I get the button back after the setFrame, it is huge. Here is the output from the log. The button thinks it is 1076101120 high!
large more btn frame: 0, 1077706752, 0, 1076101120
I checked the value of the largeButtonFrame rectangle in the debug log, and it is as I would expect.
(gdb) print largeButtonFrame
$1 = {
origin = {
x = 28.5,
y = 10
},
size = {
width = 228,
height = 30
}
}
In the end, when the view is shown in the tableView, I get this error message, and a bunch of CGContext errrors.
-[<CALayer: 0xff7b550> display]: Ignoring bogus layer size (320.000000, 1120403456.000000)
-[<CALayer: 0xff778a0> display]: Ignoring bogus layer size (300.000000, 1120403456.000000)
When my table is shown, the buttons look like the right size, but this cell basically extends forever in the table view, and I can't scroll to any of the other cells behind it.
I've also tried to set the button size different ways (e.g. set the button bounds, and then center it to the view. Or use the UIButton buttonWithType creator, then setting the frame), but none of these seem to make a difference.
Googling the error message, this error message only seems to happen when folks are trying to do animation. But I am not trying to do any animation here, just setting some buttons.
Uh yea... it's a float.
DLog(#"large more btn frame: %f, %f, %f, %f", largeMoreButton.frame.origin.x, largeMoreButton.frame.origin.y, largeMoreButton.frame.size.width, largeMoreButton.frame.size.height);
With the warnings you are getting when you're running the app, I'm not entirely sure what those are, but I'd be willing to bet you aren't using a float value for the size, that's just based on the numbers it's giving you, so I'm not 100% sure.
I'm having a scrollview as the detailedview of tableview cell. There are multiple views on the detailedview like labels, buttons etc. which I'm creating through interface builder. What I'm creating through interface builder is static. I'm putting everything on a view of height 480.
A label on my detailedview is having dynamic text which can extend to any length. The problem is that I need to set the scrollview's content size for which I need its height.
How shall I set scrollview's height provided the content is dynamic?
You could try to use the scrollview'ers ContentSize. It worked for me and I had the same problem with the control using dynamic content.
// Calculate scroll view size
float sizeOfContent = 0;
int i;
for (i = 0; i < [myScrollView.subviews count]; i++) {
UIView *view =[myScrollView.subviews objectAtIndex:i];
sizeOfContent += view.frame.size.height;
}
// Set content size for scroll view
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, sizeOfContent);
I do this in the method called viewWillAppear in the controller for the view that holds the scrollview. It is the last thing i do before calling the viewDidLoad on the super.
Hope it will solve your problem.
//hannes
Correct shorter example:
float hgt=0; for (UIView *view in scrollView1.subviews) hgt+=view.frame.size.height;
[scrollView1 setContentSize:CGSizeMake(scrollView1.frame.size.width,hgt)];
Note that this only sums heights, e.g. if there are two subviews side by side their heights with both be added, making the sum greater than it should be. Also, if there are vertical gaps between the subviews, the sum will be less than it should be. Wrong height confuses scrollRectToVisible, giving random scroll positions :)
This loop is working and tested:
float thisy,maxy=0;for (UIView *view in scrollView1.subviews) {
thisy=view.frame.origin.y+view.frame.size.height; maxy=(thisy>maxy) ? thisy : maxy;
}
A somewhat easier way to do this is to nest your layout within a view then put that view within the scrollview. Assuming you use tags, this works as follows:
UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:1];
UIView *longView = (UIView *)[self.view viewWithTag:2];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, longView.frame.size.height);
That way the longView knows how tall it is, and the scrollview's content is just set to match.
This depends on the type of content you are going to add dynamically. So let's say you have a big text data to show, then use the UITextView and as it is a subclass of the UIScrollView, you can get the setContentSize of TextView when you assign the text content. Based on that you can set the total size of the UIScrollView.
float yPoint = 0.0f;
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 320.0f, 400.0f)];
UITextView *calculatorTextView = [[UITextView alloc] init]; calculatorTextView.text = #"My looong content text ..... this has a dynamic content"; `
[calculatorTextView sizeToFit];
yPoint = yPoint + calculatorTextView.contentSize.height; // Bingo, we have the new yPoint now to start the next component.
// Now you know the height of your text and where it will end. So you can create a Label or another TextView and display your text there. You can add those components as subview to the scrollview.
UITextView *myDisplayContent = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 300.f, calculatorTextView.contentSize.height)];
myDisplayContent.text = #"My lengthy text ....";
[myScrollView addSubview:myDisplayContent];
// At the end, set the content size of the 'myScrollView' to the total length of the display area.
[myScrollView setContentSize:yPoint + heightOfLastComponent];
This works for me.
I guess there's no auto in case of scrollview, and the contentsize should be calculated for static views on the screen at least and for dynamic once it should be calculated on the go.
scrollView.contentSize = [scrollView sizeThatFits:scrollView.frame.size]
I believe would also work
I had the same situation, but then I wrote a new version in Swift 4 mirroring the better answer in Objective-C by Hannes Larsson:
import UIKit
extension UIScrollView {
func fitSizeOfContent() {
let sumHeight = self.subviews.map({$0.frame.size.height}).reduce(0, {x, y in x + y})
self.contentSize = CGSize(width: self.frame.width, height: sumHeight)
}
}