【问题标题】:UIView in UIScrollView does not appearUIScrollView中的UIView不出现
【发布时间】:2011-02-27 07:03:10
【问题描述】:

我正在尝试创建一个类似于this video 的水平滚动菜单。

由于某种原因,在将一堆UIButtons 添加到UIView 并将其添加到UIScrollView 后,它不会出现。这是我的代码(在UIViewController 的子类的-viewDidLoad 中调用):

//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];

【问题讨论】:

    标签: ios uiview menu uiscrollview uibutton


    【解决方案1】:

    您不应使用UIScrollView 的框架添加按钮。框架的原点位于超级视图(UIScrollView 的超级视图)坐标中。您应该使按钮的框架相对于UIView。因此,如果您希望按钮出现在应该从 (0,0) 开始的视图顶部。

    【讨论】:

    • 感谢 Diederik 的回复。问题是按钮的框架是相对于 UIView 的,因为 UIScrollView 是视图的子视图。所以,按钮的框架是相对于 UIScrollView 的 superview 的。也许我错过了什么?谢谢!
    【解决方案2】:

    不要将滚动视图作为子视图添加到您的视图中,而是将视图添加为滚动视图的子视图。作为-

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

    并确保您的视图的内容大小应大于滚动视图的内容大小。它对我有用。

    【讨论】:

      猜你喜欢
      • 2015-11-21
      • 2013-10-27
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多