iphone: uiscrollview doesn't scroll? but contentsize is set..? - iphone

hey people,
I have some data and want to show this in a new view inside a navigation-stack.
I created some new viewcontroller class with XIB file. Then I edited the XIB file and placed the following hierachy:
1 files owner
2 first responder
3 scrollview
3.1 view
3.1.1 label1
3.1.2 label2
3.1.3 label3
3.1.4 image4
I arranged the labels and the image. the content of the labels may differ and they should size dynamically. I also did the IBOutlets in the viewcontroller class and connected everything correctly. the files owner's view delegate is set to view..
then I load the viewcontroller and I do in the "viewDidLoad" - method the following:
- (void)viewDidLoad {
[super viewDidLoad];
self.currentPageURL.text = [self.offerItem objectForKey: #"page-url"];
self.currentPrice.text = [self.offerItem objectForKey: #"price"];
self.currentRank.text = [self.offerItem objectForKey: #"rank"];
self.currentName.text = [self.offerItem objectForKey: #"name"];
self.currentProducerName.text = [self.offerItem objectForKey: #"producer-name"];
self.currentDescription.text = [self.offerItem objectForKey: #"description"];
self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey: #"picture"] forSize:CGSizeMake(280.0, 280.0) ];
[theScrollview setScrollEnabled:YES];
[theScrollview setContentSize:CGSizeMake(320, 1200)];
[theScrollview flashScrollIndicators];
[theScrollview addSubview:theView];
}
when I start the app in my simulator, everything is shown, but if I try to scroll, nothing happens..
what do I do wrong?

Autolayout set some constraints after the view loads, so, pick your code that sets the content size in viewDidLoad and put in viewDidAppear, and your code won't be rewritten.
This worked for me.
-(void)viewDidAppear:(BOOL)animated{
[self.scrollView setContentSize:CGSizeMake(320, 1000)];
}
and sorry for my poor english.

Ensure that "Use Autolayout" is unchecked in the IB Document attributes. I had the exact same issue, setting the correct contentSize and all, but with this feature enabled, scrolling was always disabled.
UPDATE To use Autolayout, explicitly set the width and height of the scrollView's content in the viewDidAppear method, like this:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// set the scrollview to the specified size
CGRect screenRect = [[UIScreen mainScreen] bounds];
[scrollView setContentSize:CGSizeMake(screenRect.size.width, 1100)];
[scrollView setBounds:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
[scrollView setScrollIndicatorInsets:UIEdgeInsetsFromString(#"{0,0,0,-1.0}")];
}

Immediately change the frame of theScrollView to 320x480 and you should see scrolling.

Related

Add scrollview iOS Objective-C

I have the following code in viewDidLoad on my ViewController:
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:204.0/255 green:00.0/255 blue:00.0/255 alpha:1];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
autoNameLabel.text = (NSString *)[vendorDetails objectForKey:#"autoname"];
homeLabel.text = (NSString *)[vendorDetails objectForKey:#"homelab"];
descriptionTextView.text = (NSString *)[vendorDetails objectForKey:#"description"];
This all fits perfectly on the view. I know need to add an additional textView on the bottom so need the user to be able to scroll to see it. How can I add this info to a scrollable view and add my additional textView?
you could have your UIViewController extend UISCrollViewController. Or you could add a UIScrollView to your view controller's view in the method viewDidLoad. Then add all subviews of your view to the scrollview instead.
self.scrollView = [[UIScrollView all] initWithFrame:self.view.bounds]
self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.width, HEIGHT_OF_SCROLLABLE_AREA).
I would also do something like this for the scroll view height if your last textview is named lastTextView
HEIGHT_OF_SCROLLABLE_AREA = CGRectGetMaxY(lastTextView.frame + bottomPadding)
Are you sure you dragged all the other views on the scrollview? Make sure the contentSize of the scrollview is high enough(you know, higher than the screen) to scroll.
have u set delegate of UIScrollView. if you have not set delegate of UIScrollView then also it may possible that your scroll is not working.

Why does my UIPageViewController crop itself during curl transition?

In StoryBoard, I have setup two view controllers:
PageViewRootViewController - this is just blank (in storyboard) as everything is setup programmatically.
VillagerTextViewController - Storyboard sets the layout
Additionally, I have a ModelController (NSObject) that acts as the data source.
Everything is working fine. I can transition between pages, no warnings or errors and no crashes.
However, when transitioning, the bottom of the page is cropped off. I've been trying to fix it to no avail.
I can't post images yet so http://s10.postimg.org/qbjlat649/Screenshot_2013_04_20_22_12_04.png
The UIPageViewController is created in the viewDidLoad method of PageViewRootViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
if (!pageViewController) {
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
self.pageViewController.delegate = self;
VillagerTextViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self.pageViewController.view setClipsToBounds:NO];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
}
In storyboard, I have the view for PageViewRootViewController set to popover width 768 and height 200. I also display this as a popover.
The VillagerTextViewController and ModelController do not have anything (in code or storyboard) explicitly related to size, frame, or bounds.
Any help is appreciated!
EDIT:
Fixed with:
[self.pageViewController.view setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height+600)];
I don't see you doing anything in your code about giving the UIPageViewController's view a frame. It's important not to omit that! I don't guarantee that this is the source of your problem, but it might be.
self.pageViewController.view.frame = ???????
The reason this doesn't appear to be a problem is that you're covering it up, as it were, by setting clipsToBounds to NO; thus you are making yourself unaware of where the frame is. Much better, I think, not to set clipsToBounds to NO, but to get the frame right.

UITextView text not displayed when parent view is UIScrollView UNTIL I scoll

I have a UITextView inside of a UIScrollView that is configured offscreen when the code below runs in my ViewController's viewDidLoad. Unfortunately, if the "eventDetails" text is particularly long nothing is displayed inside the UITextView until I interact with it (e.g click inside and drag for example).
My question: How to have it so the text is displayed in the UITextView WITHOUT forcing the user to interact with the UITextView first?
Here is the code:
UITextView *txtDetails = [[UITextView alloc] initWithFrame:CGRectMake(-4, yOffset, page3ScrollView.frame.size.width, 0)];
[txtDetails setEditable:NO];
[txtDetails setScrollEnabled:NO];
[txtDetails setFont:[UIFont systemFontOfSize:12]];
[txtDetails resizeAndSetTextWithMaxSize:CGSizeMake(txtDetails.frame.size.width, 999999999) forText:eventDetails withAdditionalHeightOf:16.f];
[page3ScrollView addSubview:txtDetails];
CGRect frame = txtDetails.frame;
frame.size.height = [txtDetails contentSize].height;
txtDetails.frame = frame;
[page3ScrollView setContentSize:CGSizeMake(page3ScrollView.frame.size.width, txtDetails.frame.origin.y + txtDetails.frame.size.height)];
Thanks
I was able to get this working by setting the UITextView's text ONLY when needed (e.g. is about to come on screen). Below is the relevant code:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv
{
if (sv == scrollView) [self updatePagedView];
}
- (void)updatePagedView
{
int currentPage = pageControl.currentPage;
// *** loadDetailsPage3 is where I set the text ***
if (currentPage == 2) {
[self loadDetailsPage3];
}
}
If anyone has a better solution or needs more explanation just hit me up in the comments.
I've had the same issue and it's taken me several hours to find a suitable fix for it... Here's what I came up with...
-(void)DidBecomeActive {
if (!DidUpdateBounds) {
DidUpdateBounds = true; // instance variable set to false on load
NSString *oldtext = uiTextView.text;
//The trick is (1) removing it from it's superview
// (2) resetting it's 'text' property
// (3) re-adding it to the view WHILE it's on-screen.
[uiTextView removeFromSuperview];
uiTextView.text = oldtext;
[self.view addSubview:uiTextView];
[self.view setNeedsDisplay];
}
}
I'm using CoreAnimation to switch to this view. So right before I execute that code, I call this
//The 0,-300,480,300 is bounds of the UIView my offscreen UITextview is on
[self.view.layer setBounds:CGRectMake(0, -300, 480, 300)];
// SetNeedsDisplay, then call my method above to
//FORCIBILY redrew the UITextView while it's effectively on-screen
[self.view setNeedsDisplay];
[TextLogVC DidBecomeActive];
//CoreAnimation then goes here
This solves the issue. After setting the layer.bounds it redraws the control(UITextView) and it thinks it's onscreen. Then CoreAnimation takes care of animating from my current UIView to the new UIView and all of the text in the uiTextView is displayed throughout the animation.
Looks like the views that you are setting up are not refreshed.
And when you interact with the views then they are refreshed and your changes are rendered to the display...
Try adding [txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay]; after your code in viewDidLoad.
If it will help then maybe we'll find some more elegant solution...

Transparent NavgationBar on iPhone

I'm trying to display transparent UINavigationBar on top of Scrollview.
This is actual result of code that I have written...
where as I'm expecting view to be displayed like below image, which happens after I slightly scroll the image.
Code :
- (void) loadView {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self imageCount],
pagingScrollViewFrame.size.height);
pagingScrollView.delegate = self;
self.wantsFullScreenLayout = YES;
pagingScrollView.scrollsToTop = YES;
self.view = pagingScrollView;
}
question is how do I make view to load as I expected without user interacting to it?
any suggestion or help is appreciated..
EDIT: I'm creating view totally from CODE
It seems like you're trying to do this in code not in the IB. If so, you have to put your code in the viewDidLoad of the Application Delegate (e.g. MyProgramAppDeligate class or whatever). If you want it in some certain views, put it in the viewDidLoad of the UINavigationController class/subclass.
Does this satisfy your requirement?
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
And to make your statusbar translucent.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
[self setWantsFullScreenLayout:YES];
I have this inside my willWillAppear and I reset it in my viewWillDisappear.
You have to set
self.navigationController.navigationBar.translucent = YES;
As soon as you do this, the 0,0 coordinate is behind your navigation bar not below it and your view shifts behind the bar.
It may be a conflict between IB and your code. I would add the line of code suggested by Ortwin in the viewDidLoad method and then double check you've set the navBar to translucent in IB.
The code you have posted has nothing to do with the opacity of the navigation bar. Show where you are setting the configuration of the components of the view. There you could just set the alpha of the navigation bar. Alternatively if you are using nibs, just set the alpha in IB.
Since you say that it works fine after the user (you) slightly scrolls the image, the problem might be that the UINavigationBar's drawRect: method does not get called after the UIScrollView is loaded.
Suggestion: Can you explicitly call setNeedsDisplay on the navigation bar after the view is loaded?
Have you tried setting the frame of the scroll view with an Y origin of 0 after setting the nav bar to transparent?
EDIT: I mean, you don't say what's the frame used in your code.
In viewDidLoad, try moving the origin up 32 pixels and grow the height by 32 pixels as well:
pagingScrollView.frame = CGRectMake(pagingScrollView.frame.origin.x, pagingScrollView.frame.origin.y-32, pagingScrollView.frame.size.width, pagingScrollView.frame.size.height+32);
In viewWillAppear scroll the content to the correct location.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIScrollView *scrollView = (UIScrollView *)self.view;
[scrollView setContentSize:CGSizeMake(320,568)];
[scrollView scrollRectToVisible:CGRectMake(0, 548, 320, 20) animated:NO];
}

My view is displaying y=-20 dispite its frame set to y=0. After rotation it snaps back to y=0

I started by creating a universal window based app. Starting with the iPhone version I created a UIViewController and associated nib.
My App delegate:
rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window makeKeyAndVisible];
[window addSubview:rootViewController.view];
return YES;
My RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
[self.view addSubview:adBannerView];
}
I've tried instanciating buttons instead of the adBanner and I get the same result.
My RootViewController's nib has not been changed since x-code created it for me.
My MainWindow_iPhone.xib also is stock.
What's causing this?
Update
After changing the app's orientation the adBannerView (or button...) will snap into the correct place at y=0. I've tried setting adBannerView's y location to 20 presumably to compensate for the status bar and that makes everything display correctly until I change orientation. Then everything moves down 20 pixels and will leave a 20 pixel space between the adBannerView and the status bar.
Try to add the next line in your viewDidLoad (right after [super viewDidLoad];):
self.view.frame = [[UIScreen mainScreen] applicationFrame];
CGRectZero is literally a zero rect (0, 0, 0, 0), so ADBannerView should never show up if it really has a width and height of 0. You probably want to try initWithFrame:self.view.frame or so…
You should set the size identifier before adding the view:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
else
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
[self.view addSubview:adBannerView];
// now you can treat it like any other subview
// For example, if you want to move it to the bottom of the view, do this:
CGRect frame = adBannerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
[adBannerView setFrame:frame];
}
Whenever the interface rotates, you should notify the banner to change its size.
Assuming you have access to WWDC videos (which is available for free), check video session 305. It demos adding the banner.