contentSizeForViewInPopover different height in IOS4 and IOS5 - iphone

Hello,
I have UITableView with static content embedded in UIScrollView
so I have just set size in popover:
- (CGSize)contentSizeForViewInPopover
{
return CGSizeMake(600, 670);
}
but, I got different popover heights (see attachments). In IOS4.3 height is bigger than in IOS5.
I don't want to check IOS version and increase/decrease height.
Please advice.
Thanks

As far as I know, when using the navigation controller as popover contents, you have to
set the contentSizeForViewInPopover on the root controller (or anyone that is being shown as the first). (Don't try to set size on the navigation controller).
whenever you push/pop controllers and you want the popover size changed for the new controller on the stack, call setPopoverContentSize:animated: explicitly. A UINavigationControllerDelegate is good for this.

You can dynamically calculate the height of your popover based on the height of your tableview and overriding contentSizeForViewInPopoverView like this:
- (CGSize)contentSizeForViewInPopoverView {
CGFloat width = 200.0; // Currently no way to obtain the width dynamically, only height.
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
This assumes you have 1 section. If you have more, you just need to use rectForSection to determine the height of each section and add them up.

Related

How is scrollView page size calculated?

I have a scrollView embedded inside a UIView with a top constraint to the navigation bar.
My question is, how is the page size calculated of a UIScrollView (where scrollView.pagingEnabled = true)? I want each page to be the size of the aforementioned UIView, but it turns out that it's slightly bigger.
I set the contentSize as so in the view controller:
self.scrollView.contentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, (UIScreen.mainScreen().bounds.height - 64) * 4)
I figured each page size would be UIScreen.mainScreen().bounds.height - 64 (because of the navigation bar and clock) but, like I said, the page turns out to be slightly bigger.
Try to set the height of the scrollView same as UIView that hosts it self.scrollView.superview.
Edit:
The 'slightly bigger' is probably the 64 points offset. You need to set self.automaticallyAdjustsScrollViewInsets = false inside yout viewController that hosts the scrollView to fix it

Scroll View not functioning IOS 7

I have a scrollview inside which i have 20 UItextviews. The scrollview is not working. I have set the following in viewdidload.
self.MainScroll.contentSize = CGSizeMake(320, 1800);
Still it doesn't scroll. However, if i give bounce vertically, it just bounces. My scrollview is a child of the main UIview of dimension 320*600. Please guide how to enable the scroll!!
There are two ways you can get the scrolling to work.
Approach 1 (with code):
1) Pin UIScrollView to the sides of its parent view, as mentioned below.
2) Set content size of your scroll view in viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews {
self.MainScroll.contentSize = CGSizeMake(320, 1800);
}
Approach 2 (pure IB, no code required):
1) Setting contentSize is not required if using AutoLayout. You need to pin your UIScrollView to the parent view as mentioned below:
2) Then add another UIView inside UIScrollView to act as a content view and pin it to the UIScrollView and move all controls inside this content view:
3) Pin content view to its parent scroll view as mentioned below:
4) Set your UIViewController's Simulated Metrics to Freeform (this is important):
5) Size your content UIView to your desired height (obviously important too):
Apple article explaining UIScrollView and AutoLayouts:
https://developer.apple.com/library/content/technotes/tn2154/_index.html
Update the content size after some delay as below.
- (void)viewDidLayoutSubviews {
[self performSelector:#selector(updateContentSize)
withObject:nil
afterDelay:0.25];
}
-(void)updateContentSize{
UIView *viewLast = [viewContent viewWithTag:100];
scrollViewAd.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(viewLast.frame));
}

Obj-C, how do I get the height of a sub view to scale, across all iPhone sizes?

I'm having some trouble catering for the new iPhone 5 screen height, I need to resize my table view already to show an advert.
Up till iOS6 I didn't have a problem, I used the following function, but it doesn't use scale. To be honest I'm surprised it works.
+ (CGRect)setTableBoundsByHeight:(int)lHeight:(UITableView*)tbl {
CGRect tableFrame = tbl.frame;
return CGRectMake(tableFrame.origin.x,
tableFrame.origin.y,
tableFrame.size.width,
lHeight);
}
Here's the code, where I have hard coded the height of my table view at 367, that's minus the height of a navigation controller and a tab bar. 50 is the height of the advert.
if (!productPurchased) {
#ifdef VER_FREE
[[LARSAdController sharedManager]
addAdContainerToView:self.view withParentViewController:self];
[[LARSAdController sharedManager]
setGoogleAdPublisherId:#"number"];
[reportTableView setFrame:[General
setTableBoundsByHeight:(367-50):reportTableView]];
#endif
} else {
[reportTableView setFrame:[General
setTableBoundsByHeight:367:reportTableView]];
}
I've found some code which scales but I'm not sure how to implement this.
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
If this code is inside your view controller, just use self.view.bounds.height rather than 367.
By the way: You should really rename
+ (CGRect)setTableBoundsByHeight:(int)lHeight:(UITableView*)tbl
to something like
+ (CGRect)setTableBoundsByHeight:(int)lHeight tableView:(UITableView *)tbl
Ignore the scale, it scales automatically. Just check if iPhone 5 and set different height, but you use iphone5 pixel count/2 as it'll scale it to 2x itself.
Using hard-coded values (a.k.a. "magic numbers") is a wrong habit, you see why now. Always prefer using constants or runtime-computed values. Besides, it makes code easier to read because by using constants you will know what the numbers correspond to, instead of being "magic numbers" coming from nowhere.
So for your problem, compute the height value at runtime using this kind of code below.
// simply use the height of the current viewController's `view`
// which is probably the view of the `navigationController`'s `topViewController`
// and is already at the correct size, namely 367 in iPhone 3.5" and 455 in iPhone 4".
CGFloat screenHeight = self.view.height;
if (!productPurchased)
{
static CGFloat advertHeight = 50;
#ifdef VER_FREE
[[LARSAdController sharedManager]
addAdContainerToView:self.view withParentViewController:self];
[[LARSAdController sharedManager]
setGoogleAdPublisherId:#"number"];
[reportTableView setFrame:[General
setTableBoundsByHeight:(screenHeight-advertHeight):reportTableView]];
#endif
} else {
[reportTableView setFrame:[General
setTableBoundsByHeight:screenHeight:reportTableView]];
}
Note that you don't need to do any substraction yourself, as the UIViewControllers resize their view according to the space available, so that if you have for example a UITabBarController that contains a UINavigationController that itself shows a UIViewController on top of its stack, the height of this last viewController's view will be the height of the screen minus the tabBar, statusBar and navBar heights.
So instead of fetching the [UIScreen mainScreen].applicationFrame for example, and subtract the tabBar (if any) and navBar heights to have your value of 367pt, simply use the height of the viewController's view directly and you should have the right value directly.
Additional note: you should give a prefix to your second argument, thus name your method setTableBoundsByHeight:tableView: instead of setTableBoundsByHeight:: where the second argument does not have any prefix. (See #MrMage answer that suggest this too).
A better naming for your method would even be setHeight:forTableView: for example, to beter fit the Apple naming conventions.

UIScrollView setting page size dynamically

I've done quite a bit of looking into this but have been unsuccessful in finding a solution.
I have a UIScrollView called scrollView inside of UIView. I'm using this scrollView in pagingEnabled mode. Inside of scrollView, I have 3 different views created programmatically. Inside of these 3 different views, I have a bunch of stuff(UILabel,UITextView etc.). All of the views contents are dynamic and determined at runtime.So, i really don't know scrollView.contentSize. If i give the content size of this scrollView,Sometimes ,I have a screen with white blank at the botton of the screen when user scrolls down. My question is : Can i set the content size dynamically for each single page of this scrollView? For example,for page 1 :
self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2);
And set something else for page 2 as well .
I think you're confused about contentSize vs the paging size. The paging size is always the size of the scrollView bounds, and isn't a property you can otherwise set. That is, when you swipe left/right it will "page" by the width of the scrollview.
contentSize is the size of the virtual bounding box of all the subviews within the scrollview. This only serves to limit how far the scrollview will scroll, and for paging, how many times it will page, i.e. contentSize.width / bounds.size.width.
Assuming the scrollView isn't zoomed in/out (zoomScale = 1.0) then you need to position and size your subviews on the virtual 'page boundaries'. They can take up the full page boundary (be sized to match scrollview.bounds) or be inset. If you have some content that is larger/smaller then you'll have to decide if you want to change the scale of that content or size it up/down within the page bounds.
Yes you can dynamically set the content size of the scrollView. Also You can use this method:
self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2);
Nothing wrong this method. You are seeing the blank space at bottom because you ares setting the height of the scrollView's contentSize to a larger value than it's content. That's the issue. Adjust the height according to the contents, blank space will go.
please, try to get the actual content size from the current content.
the content must be some inherited class from the UIView and it has a frame.size.width and frame.size.height property.
you can use those to set the contentSize of your UIScrollview for the current content at that time when you add the content to the UIScrollView.
Yeah. You can set the scrollView size dynamically through
scroller.contentSize = CGSizeMake(1650, 2000);
You can use the following property to set the dynamic frame for each pages
[urScrollView scrollRectToVisible:frame animated:YES];
if you are using pagingEnabled = YES for your scrollview
The below UIScrollViewDelegate delegate will adjust the page and content
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = urScrollView.contentOffset.x/urScrollView.frame.size.width;
pageControl.currentPage = page;
}
//pager action
- (IBAction)changePage:(id)sender{
int page = pageControl.currentPage;
CGRect frame = urScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[urScrollView scrollRectToVisible:frame animated:YES];
}
please look at this sample app
Try out this.
- (void)viewDidLoad
{
float sizeOfContent = 0;
UIView *lLast = [scrollView.subviews lastObject];
NSInteger wd = lLast.frame.origin.y;
NSInteger ht = lLast.frame.size.height;
sizeOfContent = wd+ht;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
}

How to make this app screen scroll?

I've a screen in an app I'm coding structured like this:
View
ScrollView
View
label 1
label 2
label 3
View
UIImageView
WebView
When loaded it adds some html string into the Web View and as the whole content (labels,image,html content) is longer than the screen height, I would like to allow the screen to scroll down/up when user is reading content. This is why I added the SrollView but nothing scroll!
I also did is scroll_view.contentSize = [self.view bounds].size; but it didn't work
Any idea on how to do this ?
Thx in advance for helping,
Stephane
By default UIScrollView is set a contentSize equal to its frame size. If you want it to be scrollable, you have to explicitly set the contentSize.
scrollView.contentSize = CGSizeMake(_width, _height);
Mostly you won't be knowing the actual contents size while you create the scroll view. You can assign the contentSize after adding the UIWebview(as web view is the last view in your scroll view). You can do it like this,
// Add other views to scrollView
// Create and configure webView
[scrollView addSubview:webView];
float _width = scrollView.contentSize.width; // No change in width
float _height = CGRectGetMaxY(webView); // Returns (webView.frame.origin.y + webView.frame.size.height)
scrollView.contentSize = CGSizeMake(_width, _height);
Below code u observe Here "Height" declare Dynamically as your Requirement
EXample :
if([Myarray length]>25)
{
myString = [myString stringByAppendingFormat:#"<tr><th align=\"left\">%#</tr>",str_owes ];
height += 50;
}
webview.frame=CGRectMake(25, 153, 420, height);
[webview loadHTMLString:myString baseURL:nil];
scrollview.contentSize=CGSizeMake(0, webview.frame.origin.y+webview.frame.size.height+50);
You need to set the UIScrollView's contentSize to be the size of the two views + the UIWebView combined.
You will also need to make sure that the UIWebView if of the right size and then turn scrolling off, as you may get scrolling issues otherwise (one scroll view inside another).
More info: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html