Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView - content goes out bound when scrolling

I design the scrollview in interface builder like this

enter image description here

It looks good here. But unfortunately when I run it on emulator or device

it becomes

enter image description here

The content in scrollview is expand outside scrollview itself and even though outside UIView that contains this scrollView.

In my viewDidLoad (panel is the container of scrollView )

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat adjustPanelHeight = [PTTScreenScaleUtil getAdjustHeight:self.panel.frame.size.height];
    CGRect panelRect = self.panel.frame;
    panelRect.size.height = adjustPanelHeight;
    self.panel.frame = panelRect;

    UIImage *image = [UIImage imageNamed:@"panel-background"];
    UIGraphicsBeginImageContext(self.panel.frame.size);
    [image drawInRect:CGRectMake(0, 0, self.panel.frame.size.width, adjustPanelHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self.panel setBackgroundColor:[UIColor colorWithPatternImage:newImage]];

    NSLog(@"scrollView Height : %f", self.scrollView.frame.size.height);
    NSLog(@"scrollView contentSize Height : %f", self.scrollView.contentSize.height);
//    CGRect scrollViewRect = self.scrollView.frame;
//    CGRect scrollViewContentRect = self.scrollView.frame;
//    NSLog(@"ScrollView Height Before : %f , After : %f", self.scrollView.frame.size.height, [PTTScreenScaleUtil getAdjustHeight:self.scrollView.frame.size.height]);
//    scrollViewRect.size.width = 280;
//    scrollViewRect.size.height = [PTTScreenScaleUtil getAdjustHeight:270];
//    self.scrollView.frame = scrollViewRect;
//    [self.detailsLabel sizeToFit];
    UIView *view = [[self.scrollView subviews] objectAtIndex:0];
//    [view sizeToFit];
//    [self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
//    NSLog(@"ContentSize Height : %f", view.frame.size.height);
//    scrollViewContentRect.size.height = view.frame.size.height;
    NSLog(@"Bounds : %f", view.bounds.size.height);
    self.scrollView.frame = CGRectMake(10, 10, 280, 270);
    self.scrollView.contentSize = CGSizeMake(280, 500);
    NSLog(@"Frame Height %f", self.scrollView.frame.size.height);
    //[self.scrollView setContentSize: CGSizeMake(280, 1000)];

    CGRect termBtnRect = self.termBtn.frame;
    CGRect mailBtnRect = self.mailBtn.frame;
    CGRect twitterBtnRect = self.twitterBtn.frame;
    CGRect fbBtnRect = self.fbBtn.frame;
    termBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    mailBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    twitterBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    fbBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    self.termBtn.frame = termBtnRect;
    self.mailBtn.frame = mailBtnRect;
    self.twitterBtn.frame = twitterBtnRect;
    self.fbBtn.frame = fbBtnRect;
}

All the log return 270.0

PS. the scroll bar is correct even though the content goes outside but the scroll bar is working correctly (stay in the scrollview's frame as arrange in interface builder) I have no idea how can I solve this. Anyone help me please.

Thanks you.

like image 903
SaintTail Avatar asked Dec 06 '22 07:12

SaintTail


1 Answers

Solve it by creating new view controller in interface builder and redo the same process with careful and bingo. It works.

When I compare both two view controller I realise that the wrong one UIScrollView Clip Subviews is unchecked. When check it the problem solve.

like image 145
SaintTail Avatar answered Dec 28 '22 23:12

SaintTail