开发者

UIView in UIScrollView does not appear

开发者 https://www.devze.com 2023-02-13 22:06 出处:网络
I\'m trying to create a horizontally scrollable menu similar to that in this video. For some reason the UIView doesn\'t appear after adding a bunch of UIButtons to it and adding it to the UIScrollVie

I'm trying to create a horizontally scrollable menu similar to that in this video.

For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):

//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];

//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];

//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
    UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempBtn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    CGRect rect = CGRectMake(runningX, designPicker.frame开发者_如何转开发.origin.y, 30.0, 30.0);
    tempBtn.frame = rect;
    [buttonsView addSubview:tempBtn];
    runningX = runningX + 35;
    [tempBtn release];
}

[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];


You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).


Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-

[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];

And make sure your view should have large content size than content size of your scrollview.It worked for me.

0

精彩评论

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

关注公众号