-(void) keyboarddidhide:(NSNotification*)notif
{
if(!keyboardvisible)
{
return;
}
NSDictionary *info=[notif userInfo];
NSValue* avalue=[info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize=[avalue CGRectValue].size;
//Tag: Image Size as before
CGRect viewFrame=self.view.frame;
**viewFramesize.height+=keyboardSize.height;**
//Here I am just streaching the scrollView......
scrollView1.frame=viewframe; //Streaching the scroll view
keyboardvisible=yes;
//Tag: Image size increases....
keyboardvisible=false;
}
I am using XCode 4.
In my App I have a view page.
View
- Scroll View
- Text View
- Image View
-barButton
It is After the Notification that my image is streaching to full screen some how...
Whenevr my scroll view streches my image view stretches to the full screen.
Is there any property or method to fix the scrollview size or reset?
Your image view will stretch because you are stretching the ScrollView, if you do not want your scroll view to stretch
set the content size of the scroll view to zero.
I would suggest you to read
Related
So after figuring out how scrollView works, I've implemented it with the following code:
self.scrollView.delegate = self;
self.scrollView.userInteractionEnabled = YES;
CGRect view = CGRectMake(0, 0, 320, 750);
self.scrollView.contentSize = view.size;
The above code works as intended on ALL simulators in Xcode 6. However, when I run it my phone (iphone4s on ios7), the scroll does not function at all. Are people experiencing the same problems since the new release? Or am I missing something I've learned from the documentation?
Had the same issue here. Just need to resize the scrollview's frame size in viewDidLayoutSubviews which overrides auto layout.
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[scrollView setContentSize:CGSizeMake(320, 2600)];
// Adjust frame for iPhone 4s
if (self.view.bounds.size.height == 480) {
scrollView.frame = CGRectMake(0, 0, 320, 436); // 436 allows 44 for navBar
}
}
In AutoLayout
In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the scroll view. (This should not be confused with the intrinsicContentSize method used for Auto Layout.) To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
Here are two examples of how to configure the scroll view, first the mixed approach, and then the pure approach
Mixed Approach
Position and size your scroll view with constraints external to the scroll view—that is, the translatesAutoresizingMaskIntoConstraints property is set to NO.
Create a plain UIView content view for your scroll view that will be the size you want your content to have. Make it a subview of the scroll view but let it continue to translate the autoresizing mask into constraints:
- (void)viewDidLoad {
UIView *contentView;
contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,contentWidth,contentHeight)];
[scrollView addSubview:contentView];
// DON'T change contentView's translatesAutoresizingMaskIntoConstraints,
// which defaults to YES;
// Set the content size of the scroll view to match the size of the content view:
[scrollView setContentSize:CGSizeMake(contentWidth,contentHeight)];
/* the rest of your code here... */
}
Create the views you want to put inside the content view and configure their constraints so as to position them within the content view.
Alternatively, you can create a view subtree to go in the scroll view, set up your constraints, and call the systemLayoutSizeFittingSize: method (with the UILayoutFittingCompressedSize option) to find the size you want to use for your content view and the contentSize property of the scroll view
Pure Auto Layout Approach
To use the pure autolayout approach do the following:
Set translatesAutoresizingMaskIntoConstraints to NO on all views involved.
Position and size your scroll view with constraints external to the scroll view.
Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll view and do not rely on the scroll view to get their size.
A simple example would be a large image view, which has an intrinsic content size derived from the size of the image. In the viewDidLoad method of your view controller, you would include code similar to the code shown in the listing below:
- (void)viewDidLoad {
UIScrollView *scrollView;
UIImageView *imageView;
NSDictionary *viewsDictionary;
// Create the scroll view and the image view.
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
// Add an image to the image view.
[imageView setImage:[UIImage imageNamed:"MyReallyBigImage"]];
// Add the scroll view to our view.
[self.view addSubview:scrollView];
// Add the image view to the scroll view.
[scrollView addSubview:imageView];
// Set the translatesAutoresizingMaskIntoConstraints to NO so that the views autoresizing mask is not translated into auto layout constraints.
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
// Set the constraints for the scroll view and the image view.
viewsDictionary = NSDictionaryOfVariableBindings(scrollView, imageView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
/* the rest of your code here... */
}
I did not try Vishu's answer, but what I did was update to iOS 8 so it's compatible with Xcode 6 and it worked!
I have a scroll view that has a content view inside of it.
The scroll view has constraints to the top, bottom, leading and trailing content view and top, leading, trialing superview with the bottom space to the bottom layout.
I change the Content View width on rotation:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
_contentWidth.constant = 480;
}else{
//iPhone Portrait
_contentWidth.constant = 320;
}
}
When I run the app and rotate to landscape, the width of the Content View changes and everything looks good, but the scroll view only allows scrolling on the left half of the screen (the whole view scrolls but your finger must be on the left side of screen to scroll). The Scroll View frame = (0 0; 320 431);.
If I go to another tab on my tab bar and return to this View the frame = (0 0; 480 320); what it is supposed to be and everything works/looks fine.
Any ideas what I need to do to get this by not selecting another tab and coming back?
EDIT
I made a subclass of Content View called ContentViewSubclass and put the following method in it:
-(CGSize)intrinsicContentSize{
CGSize size;
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
size.height = 1000;
size.width = 480;
return size;
}else{
//iPhone Portrait
size.height = 1000;
size.width = 320;
return size;
}
}
Then on my ViewController.m I updated to the following code:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[_contentView invalidateIntrinsicContentSize];
[_contentView intrinsicContentSize];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{ _contentWidth.constant = 480;
_contentHeight.constant = 1000;
}else{
//iPhone Portrait
_contentWidth.constant = 320;
_contentHeight.constant = 1000;
}
}
When I log the sizes I get
Landscape:
ContentViewSubclass frame is (0 0, 480 1000)
UIScrollView frame is (0 20, 480 251)
contentView intrinsic size is {480, 1000}
Portrait:
ContentViewSubclass frame is (0 0; 320 1000)
UIScrollView frame is (0 20, 320 411)
contentView intrinsic size is {320, 1000}
Everything is in a Tab Bar View Controller and here is what it is set up like:
View
ScrollView
ContentView
Labels (many)
Textfields (many)
TableView (2)
CollectionViewSubcless (1)
I deleted the table views and Collection View in case they were causing problems and that didn't help.
Same issue. I get the correct layout on everything and it all scrolls, but you have be touching the left half of screen in landscape to scroll it. If I go to another tab view and return to this one it works correct. The scroll view then shows UIScrollView frame is (0 20, 480 300).
You're using the "Pure AutoLayout Approach" described here by Apple in a Technical Note: https://developer.apple.com/library/ios/technotes/tn2154/_index.html
The "Pure AutoLayout Approach" works in Apple's sample code because an image view has an intrinsic content size; your content view, on the other hand, does not.
If you don't want to switch to the "Mixed Approach", then your content view will have to have an intrinsic content size. You do that by creating a UIView subclass and overriding the intrinsicContentSize method. In the willRotateToInterface… method, send invalidateIntrinsicContentSize to your content view.
I struggled with a similar situation. In my case it was the rightmost section of the landscape screen-width which is "added" to the portrait screen-width when rotated to landscape (i.e. for all values of x where: w-h < x <= w). Buttons and such which were constrained to move into that area were there, but were unresponsive. If that sounds the same as what you're seeing, then this simple solution might help. It fixed it for me.
I have a view controller with a table view. The UITableViewCell width is set to 320 in IB. Now, I want to use the view controller in iPad by adding it to a superview. The superview's width is bigger than 320. The table view shows on the superview. The problem is that it only occupy 320. How can I change the cell width to occupy whole superview's width? Thanks.
Hey you need to create a custom UITableViewCell. Than you can overwrite the function
- (void) layoutSubviews
{
[dateLabel setFrame:CGRectMake(10.f, 16.f, 80.f, 12.f)];
[textLabel setFrame:CGRectMake(106.f, 16.f, self.frame.size.width-105.f + 1.f, 12.f)];
}
In that function the self.frame.size.width is the actual one.
And it works with rotation of the device, too.
I implement the grid view cell size with this method:
- (CGSize)cellSize {
return _gridMode ? CGSizeMake(100, 100) : CGSizeMake(320, 345);
}
And check
_gridMode
for determining which cell to display (either a thumbnail or a full-screen cell).
The full screen cell is supposed to be 320x345.
It draws no subviews wider than 320px.
When I draw the full screen cell, I cannot scroll and when I switch back to grid mode, I see nothing (black screen).
When I set cellSize to 310x345 instead of 320x345, it works -- except my full screen subvies get offset by a few pixels horizontally.
What should I do to display full-screen width AQGridViewCells?
I have scroll view with an UIImageView init.I add a button on UIImageView.When i zoom the image then my button is also perform zooming.How can i only zoom the Image not the button.Thanks in advance for you time.
Place the button and any other views you do not want to be affected by the UIScrollView zooming in a view at the same level as the UIScrollView. Here's an example view hierarchy that was set up for just this purpose.
The scroll view and anything inside it will zoom (in this case the Image View).
In the UIView below the scroll view (in this case, an image view with the Default.png image) will not zoom when the scroll view is zoomed.
EDIT:
To have the button remain in the same place relative to the image add it as a subview to your scroll view, add UIScrollViewDelegate to your view controller header. In your view controller implementation: capture the initial UIButton frame in viewWillAppear, and use the scroll view delegate scrollViewDidZoom method to update the button frame so that is stays in place. This will also allow the button to move with the image when panning.
Here's the updated view structure:
The applicable view controller code segments:
#interface MyViewController () {
CGRect initialButtonFrame;
...
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
initialButtonFrame = self.button.frame;
...
}
- (void)scrollViewDidZoom:(UIScrollView *)sv
{
self.button.frame = CGRectMake((initialButtonFrame.origin.x * self.scrollView.zoomScale),
(initialButtonFrame.origin.y * self.scrollView.zoomScale),
initialButtonFrame.size.width,
initialButtonFrame.size.height);
...
}
Here are a couple of images showing the button staying in place relative to its placement in the image. Note the button origin is near the top left of the red area near the center bottom of the fireman's coat when not zoomed (first image) and when zoomed (second image).
Add the button on the imageScrollView superview after the imageScrollView so that the button is not scroll view's subview.