0

I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.

I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.

I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?

Code I've gotten so far (executed in viewDidLoad of view controller):

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];

UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];

[view addSubview:scrollView];
[view addSubview:textView];

self.view = view;
DShah
  • 9,768
  • 11
  • 71
  • 127
rnystrom
  • 1,906
  • 2
  • 21
  • 47

2 Answers2

1

I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
  • 1
    Problem is that I'm doing this all in code. I guess I could use a nib, but I'd rather not at this point. – rnystrom Sep 28 '11 at 18:44
  • 1
    I know this is a much argued point, but using IB has probably saved me days or weeks of little programming mistakes, since I moved away from doing UI in code. With IB, you don't have to worry about whether the views are above, below, inside, or whether the frame or bounds are set wrong. You can even correct per-pixel anomalies, before you run your app. Of course, there are advantages to doing it in code, but it's pretty easy to move something from IB to code if you have to. – mahboudz Sep 28 '11 at 19:03
  • I'm actually about to change this controller to using a NIB. I'm running into more and more frustrations for simple stuff. – rnystrom Sep 28 '11 at 19:40
1

I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.

In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.

Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Correct, I have a UIImage subview inside a UIScrollView. I renamed the container to containerView. I tried insertSubview:atIndex: and I tried bringSubviewToFront:, neither of them worked. All I get is the UIScrollView with the UIImage. I can scroll and touch just like I could if I didn't add the other view. I don't see anything added either. – rnystrom Sep 28 '11 at 18:44
  • Fixed! The problem was setting the frame of the textView. I was setting the y to the height of the UIScrollView. Dumb. Problem now is that the UIImage inside the UIScrollView is going over the textView. Thoughts? – rnystrom Sep 28 '11 at 18:47