I have an UIScrollView and horizontally scrollable content. I can properly set up the content size, etc, but I need to place another similar ScrollView just below the first one. So, I need to make height of the ScrollView to be equal to height of its content. Currently I use this code, but it does not work. I attached screenshot with issue and the code:
//setting the correct content size of scrollView (self.contentSize is already calculated)
self.imageScrollView.contentSize = self.contentSize;
//creating temporary CGRect
CGRect frame = self.imageScrollView.frame;
//Changing the height and re-assigning new frame to imageScrollView
frame.size.height = self.contentSize.height;
self.imageScrollView.frame = frame;
Look at the screenshot: the imageScrollView is just below NavBar. The content still scrollable and the size of ScrollView is smaller than content size.
You can't set the frame's height using frame.size.height = self.contentSize.height;
Instead use frame = CGRectMake(x, y, width, height); Ex:
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.imageScrollView.frame.origin.y, self.imageScrollView.frame.size.width, self.contentSize.height);
And from the looks of things, I suspect the content size of your imageScrollView is correct, but it's being hidden by your nav bar. In which case, I recommend setting imageScrollView frame's origin to the height of the nav bar, ex:
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.navigationController.navigationBar.frame.size.height, self.imageScrollView.frame.size.width, self.contentSize.height);
Update #1: Here's a link to a great answer explaining why you can't set the frame in the way you've attempted: ios frame change one property (eg width)
So basically you can edit a lone CGRect structure by changing a single property, but you can NOT update a CGRect in that way if it's a property of another object -- in this case a property of a frame.
Update #2: Also, to change the position of elements in your interface, place the code in viewDidLayoutSubviews, not in viewDidLoad.
Update #3: To have the code in viewDidLayoutSubviews only processed once, enclose it in a conditional, ex:
bool subviewsLaidout;
- (void)viewDidLoad {
subviewsLaidout = NO;
}
- (void)viewDidLayoutSubviews {
if (!subviewsLaidout) {
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.navigationController.navigationBar.frame.size.height, self.imageScrollView.frame.size.width, self.contentSize.height);
subviewsLaidout = YES;
}
}
Related
When I'm trying to change the size of the frame a UIWebView, it changes, but the constraints that were related to this view are ignored. here is a screenshot:
link.
As you can see, the webview goes on the button, and the vertical space constraint is ignored.
Here is the code for changing the frame:
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGRect frame = _webview.frame;
frame.size.height = 1;
_webview.frame = frame;
CGSize fittingSize = [_webview sizeThatFits:CGSizeZero];
frame.size = fittingSize;
_webview.frame = frame;
}
What am I missing? What should I change to make it work?
Below are the steps.
1) Remove bottom constraints of the button. Give it height constraint.
2) Update height constraint of the web view instead of updating frame.
Hope this helps!
In your case webview changes its height accordingly but it overrides the button so if you are changing the height of webview dynamically so you must have to update the contraints of other UI components below it according to the hierarchy.
How to increase height of text view when i type return key in key board?
please give me solution .
there are many views on top.
need i to set frame of all view or just decreasing height of table view i will get solution?
Well, every time you hit 'Enter' on keyboard, you have to resize (height) the UITextView, and also resize (height) the UITableView accordingly as well.
Try this code it will set the height of the TextView according to it's content.
[txtAddress setFrame:CGRectMake(20, 90, 280, [self contentSizeRectForTextView:txtAddress])];
Method for TextView height:
- (CGFloat)contentSizeRectForTextView:(UITextView *)txtView
{
[txtView.layoutManager ensureLayoutForTextContainer:txtView.textContainer];
CGRect textBounds = [txtView.layoutManager usedRectForTextContainer:txtView.textContainer];
CGFloat height = (CGFloat)ceil(textBounds.size.height + txtView.textContainerInset.top + txtView.textContainerInset.bottom);
return height;
}
When setting new frame size, your view frame origin should still be the same but the frame height.
[textView setFrame: CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textVie.frame.size.width,<new height>)];
Consider the following UIView "MainView":
The view includes a Container View which in turn houses a UITableView controller. The container view's y coordinate starts just beneath the gradient bar. The UITableView includes the section footer at very bottom with the 'STU' label and 'chart' button.
When the UIView loads, and up-to-and-until any interaction with the tableView, MainView's dimensions are:
Frame: 0.000000x, 0.000000y, 568.000000w, 268.000000h
I have a delegate protocol set up such that tapping the chart button in the tableView will create a new view in MainView for a shadow effect via a method performing:
CGRect newFrame = self.view.frame; // self = MainView
newFrame.size.width = 100;
newFrame.size.height = 50;
UIView *backgroundShadowView = [[UIView alloc] initWithFrame:newFrame];
backgroundShadowView.backgroundColor = [UIColor blackColor];
// Do Animation
The important part above is the 'newFrame' CGRect. For some reason after interacting with the table view by tapping the chart button, or even scrolling or tapping a row, self.view.frame suddenly has the following dimensions:
Frame: 0.000000x, 52.000000y, 568.000000w, 268.000000h
And so the shadow view appears as follows, with a y origin much farther down than where it would be expected to start, just above the gradient bar.
I've adjusted the width and height of the "shadowview" for this question; normally it would be 568x268, but would extend 52 units off screen on the bottom because of this issue.
52 units is exactly the height of the statusbar (20) + navigationbar_in_landscape (32).
Of course I could manually adjust the frame dimensions, but I do not want to. I want to know why the view's frame is changing unexpectedly.
For the life of me, I cannot figure out why the view becomes suddenly offset. Any help is appreciated!!
Two comments.
(1)
This code was probably always wrong:
CGRect newFrame = self.view.frame; // self = MainView
newFrame.size.width = 100;
newFrame.size.height = 50;
UIView *backgroundShadowView = [[UIView alloc] initWithFrame:newFrame];
You surely want to define backgroundShadowView's frame in terms of self.view's bounds, not its frame as you are doing in the first line here.
(2)
The change in self.view.frame is probably illusory. You are probably checking this initially in viewDidLoad. But that is too soon; the view has not yet been added to the interface, and so it has not yet been resized to fit the surroundings.
I have a UITextView that's embedded into a UIScrollView. I would like the text view to not scroll and be exactly as high as required to show all of the text.
So the width is fixed and I set the content insets to indent the text a bit.
How do I get the correct height? I tried to set the frame's height to the content height but still it scrolls.
This should do the trick:
-(void)resizeTextView
{
CGRect frame = _textView.frame;
frame.size.height = [_textView sizeThatFits:CGSizeMake(frame.size.width, INFINITY)].height;
_textView.frame = frame;
}
I try to change the size (the width) of a table viewcontroller (the panel with text over syria):
I have tried :
-(void)viewWillAppear:(BOOL)animated
{
self.tableView.frame = CGRectMake(0, 310, self.view.frame.size.width,200);
}
but it doesn't work. Do you know how can I reduce the width of the TableView ?
Thanks for your help !
You will have to add the table view as a subview to your view controllers view with the new size in viewWillAppear
Check the view frame width to make sure it is actually less than 320 before setting it. Most likely you are setting the table view width back to 320 which is the entire width.
Also if you are just setting the width of the table I would recommend doing the following.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.width = 250;
self.tableView.frame = tableViewFrame;
}