I am working on table view based iOS application for iOS 7 and iOS8 using XIB, where I need to display UIWebView as footer view of table view. I have added UIWebView in my XIB, I don't know why I can't able to scroll full UIWebView according to the contents, it seems like locked.
Here is my code:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
webViewSocial.scrollView.scrollEnabled = NO;
CGFloat height = [[webViewSocial stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[webViewSocial stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = webViewSocial.frame;
frame.size.height = height;
frame.size.width = width;
webViewSocial.frame = frame;
tblAlerts.tableFooterView = webViewSocial;
}
The parameter of your webView delegate method is "aWebView" but you're using "webViewSocial" in the method body. If you intend to apply the implementation to whichever webView called it, you'll need to match the local variable to the name of the parameter. That is unless "webViewSocial" is an ivar (in which case I'd use the _ prefix just to avoid confusion. If it's a property of your class I'd use self.webViewSocial.
This is Swift 3 version
func webViewDidFinishLoad(_ webView: UIWebView) {
var frame = yourWebView.frame
frame.size.height = 1
let fittingSize = yourWebView.sizeThatFits(CGSize.zero)
frame.size = fittingSize
heightOfyourWebView.constant = fittingSize.height
tblAlerts.tableFooterView = yourWebView;
}
Related
I have made a semi rounded collectionView. I want to set my collectionView frame that take only half of screen but it take Full screen so I am unable to do any things.
Please let me know how to set frame for that UICollectionView.
DSCircularLayout *circularLayout = [[DSCircularLayout alloc] init];
[circularLayout initWithCentre:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT)
radius:165
itemSize:CGSizeMake(ITEM_WIDTH, ITEM_HEIGHT)
andAngularSpacing:60];
[circularLayout setStartAngle:M_PI endAngle:0];
circularLayout.mirrorX = NO;
circularLayout.mirrorY = NO;
circularLayout.rotateItems = YES;
circularLayout.scrollDirection = UICollectionViewScrollPositionRight;
// self.menuCollection.init(f: CGRect, collectionViewLayout layout: UICollectionViewLayout);
[self.menuCollection setCollectionViewLayout:circularLayout];
menuCollection is my collectionView which i want to set frame.
thanks
Assuming you are not using auto-layout, and your collection view's parent view is full screen size, you can set it as follows.
CGFloat size = self.menuCollection.superview.bounds.size;
self.menuCollection.frame = CGRectMake(0.0, size.height / 4, size.width, size.height / 2);
I have an app that uses a UIPickerView to set various parameters having to do with line style for a graph series (line width, colour, and line style). This picker is used in a table view row. I have defined the frame for the picker so that its width fills the width of the cell's contentView member. Up until iOS 9, this appeared to work. With iOS 9, there appears to be some sort of maximum cap on the width of a UIPickerView. Has anyone encountered anything similar to this?
The picker view is being created like this:
self.picker = [[[UIPickerView alloc] init] autorelease];
[self.contentView addSubview:self.picker];
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker.backgroundColor = [UIColor grouped_table_view_background_colour];
The cell is layed out as follows:
-(void) layoutSubviews
{
// we need to allow the base class to perform its layout.
static CGFloat left_margin = 40;
static CGFloat right_margin = 40;
CGSize my_size;
[super layoutSubviews];
my_size = self.contentView.bounds.size;
my_size.width -= left_margin + right_margin;
// we now need to lay out the views.
CGRect picker_rect = CGRectMake(left_margin, 5, my_size.width, my_size.height);
self.picker.frame = picker_rect;
// we want to look at the bounds of the picker
CGRect picker_bounds = self.picker.bounds;
NSLog(#"picker bounds x=%g, y=%g, w=%g, h=%g", picker_bounds.origin.x, picker_bounds.origin.y, picker_bounds.size.width, picker_bounds.size.height);
}
I am also overloading the widthForComponent method as follows:
-(CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
// we want to look at the bounds of the picker
CGRect picker_bounds = self.picker.bounds;
NSLog(#"picker bounds x=%g, y=%g, w=%g, h=%g", picker_bounds.origin.x, picker_bounds.origin.y, picker_bounds.size.width, picker_bounds.size.height);
return picker_bounds.size.width / 3;
}
I can see that the bounds of the view appear correctly both in the values that are logged as well as the background colour of the view. Despite this, the picker appears to fill less than half the width that is available (688 points on my iPad)
After [[UIPickerView alloc] init] the Picker has a size of {320,216} and this size will be used in pickerView:widthForComponent:. If you just resize the picker afterwards, the function pickerView:widthForComponent: won't be called again, so the components are stuck at their width.
Try to initialise the picker with the correct size like..
picker=[[UIDatePicker new] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 216.0)];
or call [self.picker setNeedsLayout] after you change the picker frame to force pickerView:widthForComponent: to be called again.
In generally, I can get subviews' frame in layoutSubViews:, but when I use autolayout in Xib and override it ,it doesn't work in iOS 8.3, it's always CGRectZero.
-(void)layoutSubviews {
[super layoutSubviews];
self.backIcon1.layer.cornerRadius = self.backIcon1.width / 2;
self.backIcon1.clipsToBounds = YES;
self.backIcon2.layer.cornerRadius = self.backIcon2.width / 2;
self.backIcon2.clipsToBounds = YES;
self.backIcon3.layer.cornerRadius = self.backIcon3.width / 2;
self.backIcon3.clipsToBounds = YES;
NSLog(#"layoutSubviews:%#", NSStringFromCGRect(self.backIcon1.frame));
}
In this method, I can't get real frame so that these codes don't work. I wanna know where can I get the real frame of subviews after storyboard finish autolayout.
I have a vertical scrollview with thumbnail images to act as a "side panel" for specific items on an ipad app.
Here's my code to set the content size:
- (void)setScrollViewContentSize:(UIScrollView*)scrollView{
NSInteger viewCount = [scrollView.subviews count];
NSLog(#"viewCount: %d", viewCount);
NSLog(#"height: %f", (viewCount * 190.0f));
scrollView.contentSize = CGSizeMake( scrollView.superview.frame.size.width, (viewCount * 190.0f));
}
When I run my app, there's one menu that has 57 items, so the viewCount log shows up as 57 and the height shows up as 10830. On another menu, I have 13 items and thus, the height is 2470.
Everything seems fine here, but when I scroll to the end of the view, the menu with 57 icons has way too much white space left over whereas the menu with 13 items ends perfectly on the last item with a little margin (each thumbnail is 183px). I'm trying to find out what the cause is, but it seems like the more menu items I have, the bigger the scroll view's spacing at the last item gets.
I tried calling scrollView sizeToFit and even trying to create a CGRect of a visible region to apple to my frame, but none of those worked. Any ideas?
thank you.
from https://stackoverflow.com/a/14852596/1363779
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);
This is a nice and easy way to do it.
Swift 2.0 version
var sizeOfContent: CGFloat = 0
let lLast: UIView = scrollView.subviews.last!
let wd: CGFloat = lLast.frame.origin.y
let ht: CGFloat = lLast.frame.size.height
sizeOfContent = wd + ht
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent)
I've got a UITableView holding UIWebViews of variable height, and the user has the ability to edit the content of each web view. The content of each cell (a web view) is linked to a NSManagedObject subclass that has the height of the content stored as contentHeight. When a new set of content is initially created, it is filled in with a default set of data that has a set height, so there is no issue there.
My problem arises when the user edits the content of a web view; I need to figure out the height of the new content and update the tableview accordingly. I've already set up the logic for updating the saved contentHeight variable for the corresponding object, but I cannot find a reliable method to determine the height of the new content. Below I've listed a few of the methods I've tried and haven't been able to make work. ANY help would be greatly appreciated, I've been stumped for over an hour now :(
FAILED METHODS:
[[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('sdcontent').offsetHeight"] floatValue];
CGRect webViewFrame = [webView frame];
CGRect temp = webViewFrame;
webViewFrame.size.height = 1;
[webView setFrame:webViewFrame];
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
[webView setFrame:temp];
it works but only when
making webview small (like 5px)
load it and wait for finish
calling sizeToFit on it then
getting the size THEN
advertisement: :D see my M42WebviewTableViewCell class on github which struggles with this