开发者

UIScrollView won't show in UIView

开发者 https://www.devze.com 2023-02-21 17:26 出处:网络
My app is building purely programmatically on UITabBarController above a UINavigationController, which are both declared in my AppDelegate. Inside my navigationcontroller, I\'m showing a UIViewControl

My app is building purely programmatically on UITabBarController above a UINavigationController, which are both declared in my AppDelegate. Inside my navigationcontroller, I'm showing a UIViewController, a custom class. This custom class should show a custom UIView in a UIScrollView and a UIPageControl.

Heres my problem:

self.view seems to create an EXC_BAD_ACCESS error when I call it without declaring self.view = [[UIView alloc] init] (or similar). I was wondering if this was a problem with -(void) loadView but seems like it produces the same error in -(void)viewDidLoad. So I basically had to use self.view = scrollView to even show my scrollView, considering [self.view addSubview:scrollView] produced an error. My UIPageControl should stay on the page all the time, and actually be another part of the view than the UIScrollView. So I tried to add a container-view like this

Code:

container = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
scrollView.pagingEnabled = YES;  
scrollView.showsVerticalScrollIndicator = NO;  
scrollView.showsHorizontalScrollIndicator = NO;  
// EDIT: ofcourse, I'm also resizing the frame with [scrollView setContentSize:] later, but this is farfetched code to add here.

[container addSubview:scrollView];
self.view = container;

Unfortunately, it s开发者_开发问答eems that I don't get any result at all, and what appears is just an empty view. However, if I add a UILabel or similar, it shows:

[container addSubview:[[UILabel alloc] initWithFrame:CGRectMake(50,50,50,50)]];  // entered above or beneath the addSubview:scrollView line.  

My question is: Why doesn't my scrollView appear in the UIView container?

I've noticed that some tutorials say that scrollView must have a delegate, and I agree with the logic - however I can't seem to find out how I set that delegate when I am in my CustomUIViewController-class instead of my AppDelegate.


after you change the UIScrollView size you should use:

[scrollView setNeedsDisplay:YES];

also you implement Delegates the same way you do in other classes:

.h:

@interface MyClass : NSObject <UIScrollViewDelegate>


Okay, the problem seemed to be the initialization - I didn't realize that frame and content was two different things. Seems like the frame that is initializing the view should be whatever size the view should fill, while content is the actual content of whatever should be scrolled. So when I was having problems with user interaction, it was really this.

The problem of why it didn't show in the first place was (stupid.) that the frame was initially, and never changed from, 0,0 so I really lied in my first post.

Thanks to UIScrollView and PageControl: space between views who solved my problem with user interaction.

My steps was to backtrace from self.view:
NSLog(@"%f\n%f", ((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.width, ((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.height);

when I realized these were 0 and 0, fixing the problem wasn't too hard :) thanks though, for your efforts Kristian.

0

精彩评论

暂无评论...
验证码 换一张
取 消