I am developing in Objective-C. I am using the following code for scrollView, and it works fine in Portrait orientation.
- (void)viewDidLayoutSubviews
{
UIScrollView *scrollView = self.scrollview;
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
}
But the view does not slide down when I change to landscape orientation.
Am i missing something?
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
In this, 320 used for Width.. When you are trying for landscape orientation. Then please increase size for width..
Please check it with more than 320 (Minimum Screeen Width).
3.5 inch - 480 & 4 inch - 568
[scrollView setContentSize:CGSizeMake(SCREEN_WIDTH + YOUR_EXTRA_SIZE,3100.0)];
Hopefully, it will help you.
Thanks.
- (void)viewWillAppear:(BOOL)animated
{
UIScrollView *scrollView = self.scrollview;
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
}
Scrollview always loaded in portrait mode in viewDidLoad/loadView method, so what ever adjustment you like to made you have to do those changes in viewWillAppear/viewDidAppear methods.
When Device get rotate didRotateFromInterfaceOrientation method get called, so I would suggest to change the ScrollView Contant size this way
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
self.scrollView.contentSize = self.contentView.bounds.size;
}
Note : Make sure your have UIScrollView and UIView in your layout
UIScrollView is scrollView and
UIView is contentView
Related
I have a webView inside a scrollView (scrollView > mainView > webView and a view2 containing other components). In order to have only one scroll (the scrollView one) I calculate the height of the webView when it finish loading then I set the height of the mainView (its height is equal to the the webView + the view2 heights). My application supports the portrait and the landscape modes. To check when the rotation has changed I use:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Inside this function I call another one in order to reset the content of my webView (set the html content) so that the
(void)webViewDidFinishLoad:(UIWebView *)webView.
can be recalled:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//set the main view height
[self.loader stopAnimating];
CGSize contentSize = webView.scrollView.contentSize;
float scrollHeight = contentSize.height + 270;
NSLog(#"webViewDidFinishLoad scrollHeight %f", scrollHeight);
self.mainViewHeight.constant = scrollHeight;
}
My problem: when the application is at first in the portrait mode, the scrollView height is correct and works perfectly. When after that I change the rotation to the landscape mode, also the height is correct, but when I return the device to the portrait, the scrollView keep the height of the landscape mode even if I set the height in the webViewDidFinishLoad. When I put a breakpoint in self.mainViewHeight.constant = scrollHeight; I found that the contentSize height doesn't change when I go from the landscape to the portrait mode
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// change the frame for both portrait and landscape
// check the orientation
if(toInterfaceOrientation == UIDeviceOrientationPortrait){
webView.frame = self.view.frame;
}else {
//
}
}
The above contents are placed in a scrollview but the output in the app is as below
If i place these contents outside the scrollview it is working fine, it was working fine before update to xcode 8
Try adding these constraints for UIScrollView
Don't change anything layout (Autoresizing/Autolayout). I think the issue is scrollview content size. So, you update the view frame in viewDidLoad method. And after then update scrollview content size.
- (void)viewDidLoad {
[super viewDidLoad];
//set view frame
self.view.frame = [UIScreen mainScreen].bounds;
}
I have a UIView,
UIView *topBarView
I am deciding the size i.e. the width and height of topBarView as
CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height*.05);
Now I am setting the frame of topBarView as
topBarView.frame = (CGRect){0, 0,topBarSize};
Im my view controller I have set this condition for rotation
- (BOOL)shouldAutorotate
{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
When my view controller opens in portrait mode it is all good and as expected the topBarView is placed at the top
But when my view opens in Landscape Left mode the topBarView instead of being on top in Landscape is on the left of the screen i.e same frame as it was in case of portrait
How can I fix this?
As you have set frame your view's origin is (0,0) so it will definitely set in top - left corner in any orientation.
If you want to manage width and height according to orientation then you should use autolayout.
You should set top,leading,trailing and fixed height constraint to that view. so, it will will manage height,width and position with orientation.
Update:(as asked in comment)
Write below code in viewdidload and comment yours
UIView *topBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.5)];
topBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview: topBarView];
Hope this will help :)
My storyboard has UIImageView which is automatically positioned and scaled by autolayout constraints.
When application runs, this UIImageView is properly resized and positioned according to device screen size and autolayout constraints.
How I can get UIImageView frame after it is displayed on the screen?
Common UIImageView.bounds and UIImageView.frame values return original values which were used in storyboard and does not reflect new UIImageView size.
In order to get the right frame/bounds of your UIImageView after resizing, you need first ask auto-layout to update that layout using [yourImageView layoutIfNeeded]. that will solve your constraints and update your yourImage.bounds.
[myImageView layoutIfNeeded];
NSLog(#"w: %f, h: %f", myImageView.bounds.size.width, myImageView.bounds.size.height);
Check these methods:
- (void)viewWillAppear:(BOOL)animated {
// view is about to be added to hieararchy
}
- (void)viewDidAppear:(BOOL)animated {
// view was added
}
- (void)viewDidLayoutSubviews {
// VC just laid off its views
}
I want to have an UIView with width=480 and height=640, centered in the middle of my master view.
I want this to happen both in landscape orientation and portrait orientation, but now if I design it for portrait orientation, when I'm turning the device to landscape, my UIView has width=640 and height=480.
Any solutions for my UIView to keep it's sizes in both orientation types?
you can implement viewDidLayoutSubviews (in UIViewController) or layoutSubviews (in UIView) to reposition the view.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.myCustomView.bounds = CGRectMake(0, 0, 480, 640); // update size
self.myCustomView.center = self.view.center; // update center
}