0

I have built an iPhone app with a page to show the contents of a .txt file in UIScrollView. For some reason my plain txt file causes the Scrollview to allow it to pan slightly to the left and right, I want it to be solid up down scrolling only.

Really need help with this one..

Thanks

#import "ITFAQController.h"
#import "FTCoreTextView.h"

@implementation ITFAQController

- (NSString *)textForView {

NSString *path = [[NSBundle mainBundle] pathForResource:@"faq" ofType:@"txt"];
NSError *error;
return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding         error:&error];
}


/*- (id)init
 {
 self = [super init];
 if (self) {
 // Custom initialization
 [self showLogoInNavBar:YES];
 }
 return self;
 }
 */
#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self showLogoInNavBar:YES];

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 330, (self.view.bounds.size.height - 90))];
[self.view addSubview:scrollView];

FTCoreTextView *ctView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(10, 0, 300, scrollView.bounds.size.height)];
[ctView setText:[self textForView]];


FTCoreTextStyle *defaultStyle = [FTCoreTextStyle styleWithName:FTCoreTextTagDefault];
[defaultStyle setFont:kITDescriptionFont];
[defaultStyle setTextAlignment:FTCoreTextAlignementJustified];
[defaultStyle setParagraphInset:UIEdgeInsetsMake(5, 10, 0, 0)];
[ctView addStyle:defaultStyle];

FTCoreTextStyle *title = [FTCoreTextStyle styleWithName:@"title"];
[title setFont:kITTitleFont];
[title setColor:kITCellFontColor];
[title setParagraphInset:UIEdgeInsetsMake(10, 0, 5, 0)];
[ctView addStyle:title];

FTCoreTextStyle *bold = [FTCoreTextStyle styleWithName:@"bold"];
[bold setFont:[UIFont boldSystemFontOfSize:14]];
[ctView addStyle:bold];

FTCoreTextStyle *link = [FTCoreTextStyle styleWithName:FTCoreTextTagLink];
[link setColor:[UIColor blueColor]];
[ctView addStyle:link];

FTCoreTextStyle *bullets = [FTCoreTextStyle styleWithName:FTCoreTextTagBullet];
[bullets setBulletColor:kITCellFontColor];
[bullets setFont:kITItalicFont];
[bullets setParagraphInset:UIEdgeInsetsMake(0, 10, 0, 0)];
[ctView addStyle:bullets];
[ctView fitToSuggestedHeight];

[scrollView addSubview:ctView];
[scrollView setContentSize:[ctView suggestedSizeConstrainedToSize:CGSizeMake(scrollView.bounds.size.width, CGFLOAT_MAX)]];
//[ctView fitToSuggestedHeight];
//[ctView sizeToFit];
}


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
Steve
  • 151
  • 2
  • 12
  • 1
    While not directly relevant to this problem (Tom has the correct answer below), it's also worth knowing about two properties of `UIScrollView`: `alwaysBounceHorizontal` and `alwaysBounceVertical`. If `bounces` is set to `YES`, these control whether the scroll view still bounces if the `contentSize` is smaller than the `frame`. They default to `NO` (except in `UITableView`, I believe), but if you want to *enable* this sort of behavior, that's one thing you can do. – drewmm Apr 03 '13 at 14:57
  • Well I partly solved the problem by preventing bouncing and locking while scrolling. Though there is no vertical bouncing either, its better than it use to be. I still find I can move left and right by 5 or 10 pixels. `UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, (self.view.bounds.size.height - 90))]; scrollView.bounces = NO; scrollView.alwaysBounceHorizontal = NO; scrollView.directionalLockEnabled = YES;` – Steve Apr 03 '13 at 15:33

2 Answers2

2
CGRectMake(0, 0, 330, (self.view.bounds.size.height - 90))];

Replace 330 with 320. That should do the trick.

Edit: As written in the comments you should prefer self.view.bounds.size.width

Git.Coach
  • 3,032
  • 2
  • 37
  • 54
  • 2
    More generally, replace 330 with `self.view.bounds.size.width`. The explicit number works because all iOS devices have the same width, but it's always better to generalize to cope with the possibility of future devices with different sizes. – drewmm Apr 03 '13 at 14:49
  • I tried below but still allowed to pan left & right... `CGRectMake(0, 0, self.view.bounds.size.width, (self.view.bounds.size.height - 90))];` – Steve Apr 03 '13 at 14:57
  • Is your scrollview as width as the whole screen? – Git.Coach Apr 03 '13 at 15:05
  • I applied self.view.bounds.size.width thanks for your help Tom – Steve Apr 03 '13 at 16:14
0

The problem likely has to do with this line:

[scrollView setContentSize:[ctView suggestedSizeConstrainedToSize:CGSizeMake(scrollView.bounds.size.width, CGFLOAT_MAX)]];

I don't know what that method does, but it's probably giving you a contentSize with width > 320. Adding the following lines after that would fix it, I think:

if (scrollView.contentSize.width > self.view.bounds.size.width)
    [scrollView setContentSize:CGSizeMake(self.view.bounds.size.width,scrollView.contentSize.height)];
drewmm
  • 737
  • 6
  • 17