Set the starting point of a horizontal UIScrollView - ios

I've tried many different solutions but none of them are working for me..
I have a horizontal scroll view, now it starts at the left, but how can i make it start at the middle?
I'm using this code right now in the Controller:
- (void)viewDidLoad {
[scroller2 setScrollEnabled:YES];
[scroller2 setContentSize:CGSizeMake(1735, 300)];
[super viewDidLoad];
}
and
IBOutlet UIScrollView *scroller2; in the header file.
I'd made the content in interface builder (.xib file), and i'm using Xcode 4.2!
Thanks in advance!

You are looking for the contentOffset property.
[scroller2 setContentOffset:CGPointMake(appropriateXDisplacement, 0)];

I think you need to call scrollRectToVisible:animated:
try this in viewDidLoad or viewDidAppear or sth. similar.
[scroller2 scrollRectToVisible:CGRectMake(1735/2.0, 300/2.0, 1,1) animated:NO]

Related

UIStackView not reversed when viewDidLoad in RTL language

I have a UIStackView in Storyboard with structure as below and want to scroll it to a specific button in viewDidLoad.
This is how I'm scrolling it.
[self.scrollView scrollRectToVisible:button.frame animated:YES];
The problem is, when setting system language to a RTL language (such as Arabic), the direction of UIStackView and button.frame are still left to right in viewDidLoad, so the scroll position is incorrect.
What should I do to fix this?
As you have seen, frame setup is not completed in viewDidLoad.
Move that code to viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.scrollView scrollRectToVisible:button.frame animated:YES];
}

iOS - UIScrollView setContentSize in separate method

I have a UIScrollView that works perfectly when I set its content size in the viewDidAppear method, just like this:
-(void)viewDidAppear:(BOOL)animated{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1500)];
}
But what I actually want to do is to calculate the height of my view after I've added all the different subviews in it, and that happens in a method "getAvis".
But when I add these two EXACT SAME LINES, just like this:
-(void)viewDidAppear:(BOOL)animated{
[self getAvis];
}
-(void)getAvis{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1500)];
}
It doesn't work anymore...What is the explanation? I really need to be able to set the content size in a separate method to be able to modify the content size whenever I want!
Thanks
Just like #3CC and #YuviGr pointed out, calling [super viewDidAppear:animated] first in the viewDidAppear method worked...Sometimes life is easy and I am stupid. Thanks guys

UITableView moving down after navigate back from next page

I have a tableview with frame size of(0, 65, 320, 503).When i navigate to another page then i come back it move down.
add the below line in your - (void)viewDidLoad method.
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.
I hope you found a solution that worked for you. This was happening to me when using AutoLayout and UITableViews combined with the wonderful SWRevealViewController class. Long story short I wasted a bunch of time and eventually found this answer that worked for me:
https://stackoverflow.com/a/23021157/892990
I moved my UITableView to a lower position in the view hierarchy (by putting a dummy 1pt-thick spacer UIView between it and the Top Layout Guide), and now it behaves as expected.
Place this code in view will appear,
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
[self.navigationController.navigationBar setTranslucent:NO];
and also try removing auto layouts.
Hope this helps...
I think you need to set frame of tableview in viewWillAppear method again because some way in your code you r changing the frame so just set it in viewwillappear .
Try this in your viewWillAppear
[your_tableview removeFromSuperview];
[your_tableview setTranslatesAutoresizingMaskIntoConstraints:YES];
your_tableview.frame = CGRectMake(0, 65, 320, 503);
[self.view addSubview:your_tableview];
You could also try this if you don't want to use the header section
your_tableview.tableHeaderView = nil;
if you are using storyboard.
1>set Navigation bar's translucent property to NO. Adjust your subviews frame again because after setting its NO ,the subview will be adjusting with some new frames.
2> In viewDidLoad set the following
self.automaticallyAdjustsScrollViewInsets = NO;
3> its done.

UIScrollView stops scrolling after adding UILabel on Storyboard

I encountered a strange problem while working with the UIScrollView and UILabel.
My viewDidLoad function looks like this:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
scrollViewContent.contentSize = CGSizeMake(320, 960);
scrollViewContent.scrollEnabled = YES;
}
If I add UILabel on Storyboard scroller stops scrolling.
Also i added label as subview but this does not change anything
[scrollViewContent addSubview:labelHeader];
Does anyone had a similar problem or know a solution?
I've encountered this problem once and solved it with this simple fix:
In storyboard mode first select your UIScrollView and then open the "Resolve Auto Layout Issues"-menu. Click the option: "Add missing constraints".

Add clickable and fixed subview to UITableViewController?

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:
[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];
The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.
Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?
Thank you in advice!
Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!
First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.
But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:
As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)
So, my code is now looking like this:
#synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
[self resizeTableToFitBanner];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bannerView];
// some other code
}
BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:
https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html
I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

Resources