Scroll view with Auto Layout - ios

I have a view in scrolling view, and when I seque(push) from this ViewController to CommentViewController, and came back, view in scroll view moved for 200 px up, I dont know why, I don`t anything in code.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 2000)];
}
Its when Auto Layout is on. When it off, its work, but design is not impressive...
Can I fix it with Auto Layout?

Here's a quick fix
In viewWillDisappear, save the scrollView's contentOffset and reset it to zero.
Later in viewDidLayoutSubview, restore the contentOffset to old value.
Here's the sample code
-(void) viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!CGPointEqualToPoint(CGPointZero, self.contentOffset))
{
scroller.contentOffset = self.contentOffset;
self.contentOffset = CGPointZero;
}
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.contentOffset = scroller.contentOffset;
scroller.contentOffset = CGPointZero;
}

Related

UIScrollView content size out of bounds in ios7

I have a wired bug in iOS7, my UIScrollView content height is out of screen bounds and I can't scroll down to the bottom.
I'm using AutoLayout.
The view heirarchy is:
- UIView
- UIScrollView
- UIView
- Buttons
In iOS8+ it's working fine.
I have this in viewDidLoad:
if ([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
And this also:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(320, 1380);
}

Zooming out from content of UIscrollview

I used to use these two lines to zoom out on the content of my scrollview. However since iOS8, I can't zoom out anymore. It does nothing.
Can someone explain to me why. I have autolayout turned off.
[scrollView setZoomScale:0.3 animated:YES];
scrollView.contentSize = CGSizeMake(tempSize.width*0.3, tempSize.height*0.3);
I am not sure if this will help you, but this method works for me when zooming in iOS 8. In this code, an imageView is in the scroll view.
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 6.0;
self.scrollView.contentSize = self.PhotoView.frame.size;
self.scrollView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.PhotoView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView*)view atScale:(CGFloat)scale{
}

iOS custom keyboard loading issue

I am creating a custom keyboard on iOS using Objective-C. To increase the height of the keyboard to make it same as default keyboard height (with suggestions bar) for both portrait and landscape, I have added height constraints programatically { I removed the previously added constraint before adding the new one }.
I am updating the height in viewWillLayoutSubviews method of the class so that on orientation change, the desired height of the keyboard is achieved and it is working perfectly fine.
But my issue is that on orientation change, the view does not autolayout perfectly as it does in default keyboard on iOS, rather the view changes and then updates the height and then expands to the right of the screen to cover the complete area. ( basically a jerk happens before the keyboard completely loads itself )
Also, on initialization of the keyboard, the view is loaded with the default height and then it updates the custom height. Post that, it applies the auto layout constraint and updates the UI, hence providing the complete layout of the keyboard screen. The whole process looks a bit jerky.
I am using xib with auto layout.
In case I do not update the custom height of the keyboard, the above mentioned jerks are solved but the height of the keyboard remains default allowed by apple.
There is another thing that there are a lot of views in XIB and hence a lot of constraints. If I remove the constrains, the keyboard loads pretty quick and the custom height is also achieved. But as soon as I rotate the device, the view does not layouts and I am stuck with half keyboard on the screen.
Any suggestion would be great!
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Perform custom initialization work here
if (IS_IPAD) {
self.portraitHeight = 300;
self.landscapeHeight = 300;
}else{
self.portraitHeight = 257;
self.landscapeHeight = 203;
}
}
return self;
}
- (void)updateViewConstraints {
[super updateViewConstraints];
// Add custom view sizing constraints here
if (_viewWillAppearExecuted)
[self adjustHeight];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[self.view addConstraint:self.heightConstraint];
_viewWillAppearExecuted = YES;
}
#pragma mark - Setters/Getters
- (NSLayoutConstraint *)heightConstraint
{
if (!_heightConstraint) {
_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:self.portraitHeight];
_heightConstraint.priority = UILayoutPriorityRequired - 1;
}
return _heightConstraint;
}
#pragma mark - Methods
- (void)adjustHeight
{
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
return;
[self.view removeConstraint:self.heightConstraint];
// [self.imageView removeConstraint:self.heightConstraint];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat screenH = screenSize.height;
CGFloat screenW = screenSize.width;
BOOL isLandscape = !(self.view.frame.size.width ==
(screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
self.isLandscape = isLandscape;
if (isLandscape) {
self.heightConstraint.constant = self.landscapeHeight;
// [self.imageView addConstraint:self.heightConstraint];
[self.view addConstraint:self.heightConstraint];
} else {
self.heightConstraint.constant = self.portraitHeight;
// [self.imageView addConstraint:self.heightConstraint];
[self.view addConstraint:self.heightConstraint];
}
}
Try this codes for your problem. First of all do some R&D with this code and let me know.

My app crashes after navigating backwards from a scrolled UIScrollview in a Navigation controller

It is a bit tricky to explain, but I will try. When I navigate backwards from the "Mercurial page" (see picture) when the scrollview is scrolled, my app crashes. But when it is not scrolled, the app does not crash... I have implemented <UIScrollViewDelegate> Any help would be greatly appreciated! Here are some pictures and some code that I have in my app:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self.scrollView layoutIfNeeded];
self.scrollView.contentSize=self.contentView.bounds.size;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat yPos = -scrollView.contentOffset.y;
if (yPos > 0) {
CGRect imgRect = self.imageView.frame;
imgRect.origin.y = scrollView.contentOffset.y;
imgRect.size.height = HeaderHeight+yPos;
self.imageView.frame = imgRect;
}
this worked for me.
-(void)viewWillDisappear:(BOOL)animated{
[self.scrollView setDelegate:nil];
}

UIScrollView setContentSize doesn't work

I have a really big problem with UIScrollView. To make sure it wasn't my actual project, I created a NEW project, iPhone only, for iOS 6 and 7. I disabled autolayout.
I created a Tab Bar project
I created a view controller,
embedded it in a navigation controller and connected it as Tab bar
item #0
I put a UIScrollView in my view.
I connected the scrollview in the custom UIViewController created for this view.
I synthesized my IBOutlet and set the delegate to self. I also
implemented delegate in .h
Here is my code (ViewDidLoad):
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
NSLog(#"contentSize width %f", self.scrollView.contentSize.width);
NSLog(#"contentSize height %f", self.scrollView.contentSize.height);
NSLog(#"%#", NSStringFromCGAffineTransform(self.scrollView.transform));
It returns me "contentSize width 20000.000000", "contentSize height 0.000000
" and "[1, 0, 0, 1, 0, 0]". It scrolls left-right where it should scroll up-down
Please help!
I had the same issue until I realized I hadn't set the UIScrollView delegate. Also, if you're using auto layout, you need to wait for the layout to be applied to the UIScrollView. This means you should set the Content Size in "viewDidLayoutSubviews".
In viewDidLoad method, frame may not be correct, move the code to this method:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Your code
}
Use this code. ScrollView setContentSize should be called async in main thread.
Swift:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async {
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
}
}
Objective C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
scrollView.contentSize = contentRect.size;
});
}
Okay, there is absolutely nothing wrong with your code. So some suggestions:
Make sure you added to your .h file like so:
#interface yourViewController : UIViewController <UIScrollViewDelegate> {
}
Try moving this snippet of code to the "viewWillLoad" method:
- (void)viewWillAppear:(BOOL)animated {
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var contentRect = CGRect.zero
for view: UIView in scrollView.subviews {
if !view.isHidden {
contentRect = contentRect.union(view.frame)
}
}
scrollView.contentSize = contentRect.size
scrollView.contentSize.width = self.view.frame.width
}
Working Swift 3.X code. Converted #Karen Hovhannisyan's answer.

Resources