Resize UITableView and Today Extension dynamically - uitableview

After trying this to dynamically resize my UITableView according to its contents size and failing:
self.tableViewPairedDevices.sizeToFit()
I tried this which worked fine as long as my Today Widget is tall enough to show it entirely:
var frame:CGRect = self.tableViewPairedDevices.frame
frame.size.height = self.tableViewPairedDevices.contentSize.height
self.tableViewPairedDevices.frame = frame
My next challenge was to get my iOS 8 Today Widget to resize according to the UITableView size, which I almost solved with:
self.preferredContentSize = self.tableViewPairedDevices.contentSize;
self.tableViewPairedDevices.setTranslatesAutoresizingMaskIntoConstraints(false)
The problem is that only one of them works at a time, if the UITableView gets resized, the extension's view doesn't, if I comment out the code that resizes the UITableView, the Today Extension gets resized successfully (the weird thing is that it knows what size the UITableView should be, even though the UITableView itself hasn't been resized) but the UITableView doesnt.
Any suggestions on how to achieve both things?
Thanks a lot!

I was able to make the above code work perfectly by disabling AutoLayout on the Extension's view

Don't forget UITableView has a Scroll inside. You need to define the height somehow.
I add a height constraint to the UITableView and then I updated that value to the contentSize.height
And that works.

Related

UICollectionView Not Showing Cells Completely. Last Cells Are Not Getting Shown Completely

I have created a custom gallery with collection view which shows all the Photos of camera roll from photos app. I have made a custom cell class. Now the thing is happening like below - : It is not showing my last row of cells. Please folks help me out with this.
It could be great for me. I am not using Constraints for my UICollectionView. I am not able to figure out ; how to give content size to UICollectionView like UIScrollView.
Guys some time problem seems way bigger than they looks.
I figure it out and yes it solved mine.
I was searching the simple and short solution for this and I found same related question below - :
UICollectionView not able to scroll to see the entire last row
and the answer given by #Carpetfizz really worked for me.
I was using auto layout thats the reason it was solved ;
If you are not using auto layout it won't work for you.
Please enable Auto layout in your xib file
If you are using constraints, create NSLayoutConstraint IBOutlet for height of collection view. Then do this thing
[collectionView reloadData];
layoutConstraintCollectionViewHeight.constant = collectionView.collectionViewLayout.collectionViewContentSize;
It will automatically resize you collection view height and can scroll upto last item.
For non-constraint design, use this
collectionView.frame = CGRectMake (x,y,w,h); // Here calculate your height and width for collection view
[collectionView reloadData];

How to resize a UIScrollView to fit its contents? ios xcode

In my application( for iphone and ipad),I have
1.MPmoviePLayer
2.tableView
3.TextView
4.CollectionView,all in a single view
The number of entries in the tableView and CollectionView may vary.The textView may also have variable content depending upon the main data.
All the four views are added as subviews for a scrollView.I want the scrollView to automatically resize its height depending on the content.I want the app run in iOS versions from iOS 7.Is there a way to do that?Using autolayout may work fine in iOS 8.But i think it will not work in iOS 7.Any suggestion is accepted.
Thanks in advance.
set below simple things in your appropriate method.
int height = CGRectGetHeight(tableview.frame)+CGRectGetHeight(CollectionView.frame)
+txtview.contentSize.height;
scrolview.contentsize = CGSizeMake(CGRectGetWidth(self.view.frame), height);
try this in your scrollViewDidScroll method...may be its work...
self.scrollview.contentSize = CGSizeMake(self.scrollview.contentSize.width,self.scrollview.contentSize.height);

UITableView + UIWebView within UIScrollView

I'm struggling with wrapping my head around using UITableView with UIWebView within UIScrollView. The interface I'm trying to build consist of a header (containing a few labels, always the same size), UIWebView (can be any height, I'm scaling it dependently on the content) and a UITableView (also can have any number of elements). I want user to scroll through the whole content like he would be scrolling through one page.
I realize that Apple doesn't recommend using UITableView or UIWebView within UIScrollView. However, I'm having a hard time to think about some workaround.
Is it possible to get the desired effect done the way I'm trying to do it right now? If not, what would be the cleanest or at least working way of doing it?
(I'm currently using Xcode 6 beta 3 / swift)
You can set your UIScrollView's contentSize.height to the sum of your UITableView's contentSize.height and the height of your UIWebView's content.
To get it, just add this to your ViewController after setting it as your UIWebView's delegate:
func webViewDidFinishLoad(webView: UIWebView!) {
let output = webView.stringByEvaluatingJavaScriptFromString("document.body.offsetHeight;")
let contentHeight = (output as NSString).floatValue
}
Then, you just have to set the frame height of your tableView and webView to their content's height and place them correctly in the scroll view.
Hope this will help,

How can I change UITableView height to its content height and still have clickable content?

So I have a tableview where I changed the size to match its content size with this code.
CGRect testF = table1.frame;
testF.size.height = table1.contentSize.height;
table1.frame = testF;
But when I try to click any of the items I can only click the top two or the section in the red, because that is the initial height of the table in the beginning.
Is there a way around this, to dynamically change its height and still keep it clickable?
I would think that the problem is not that you re sized the tableview, but that there is something that is blocking those touch events. Perhaps you have some other view in the way or the tableview is nested inside of another view which has a frame set to the tappable area that you see but has clips to bounds off so you are able to see the tableview outside of it. Try doing something like the following and see if it works, this should test my theory out:
table1.superview.frame = cgrectMake(table1.superview.frame.origin.x, table1.superview.frame.origin.y, testF.size.width, testF.size.height);
Add this under the code you showed above.

UITableView in storyboard not updating content size on rotation

I'm working on project targeted for iOS 6 that leverages storyboards and auto layout. In the storyboard there are many places where a UITableView is added as a subview to a view controllers view. This table view uses prototype cells from the storyboard.
The issue we're running into is that if the view controller is initially loaded in landscape orientation and the device is then rotated to portrait, the table view begins to scroll both vertically and horizontally. The table views cells are drawn with the correct dimensions but there is additional white space to the right.
It appears that while the frame and bounds of the table view are being updated to the correct size on rotation, the table views content size is not. Regardless of any update rotation change the content size remains the same dimensions.
The issue doesn't present itself if programatic table view cells are used.
A few garish work arounds I've found, 1.) calling reloadData or reloadRowsAtIndexPaths:withRowAnimation: 2.) manually setting the property contentSize.
Both of these seem less than ideal.
I've added this
link to a dead simple sample project which demonstrates this issue. The only changes made are to the storyboard and the main view controllers implementation.
Before rotation
After rotation
I'm having the same issue - can't seems to find any documented answer related to this. I ended up manually modifying the UITableView contentSize like you mentioned in:
- (void) viewWillLayoutSubviews
{
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.height, self.tableView.contentSize.height);
}
I ran into this issue today and filed a bug report with Apple.
Appears that if you are using a custom cell with a UI element AND autoLayout, the UIScrollView content size is having problems.
If you remove all UI elements, OR turn off autoLayout, OR use a factory cell (basic, etc), all works fine.
Same issue I have rectified in my project.
I guess this is a bug in Storyboard.
Then I have solved it by manual coding in willAutorotate method by setting
tableview.contentsize = CGSizeMake(tableview.width, tableview.contentsize.height);
Hope this will work for you as well.
If you find any apple documentation regarding the same then please update me as well. Till then you can use the same solution.
Appears that if you are using a custom cell with a UI element AND autoLayout, the UIScrollView content size is having problems.
I had to turn off AutoLayout for my custom UITableViewCells to be able to scroll to the bottom on updating the data and then [self.tableView reloadData].
With AutoLayout turned on, the tableView.contentSize was being updated, but I still wasn't able to scroll to the bottom unless I rotated the device.
I found the following to work for me:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^{
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, self.tableView.contentSize.height);
});
}
Notice the async dispatch: if that line would be executed synchronously then the contentSize change would trigger another layout pass before the current one would have completed. This triggers an exception:
Auto Layout still required after sending -viewDidLayoutSubviews to the
view controller.
Usage of Constraints helped me. Since you are using Storyboards, it is really easy to set Constraint values for all edges, so UITableView will always fill the whole ViewController (of course if UITableView fills whole ViewController) regardless of device orientation.
I had the same problem.
I found this link. When I tried to implement this I did not find the Auto-sizing attributes for my view then I clicked on Master View Controller and then clicked on the File Inspector and uncheck Use Autolayout and then go to Attributes inspector auto-resizing should be there then you can change the attributes how you want it.
I am sure you must have managed to figure this out.

Resources