iphone - 将 UIButton 添加到 UIScrollView,然后将 UIScrollView 添加到 UIView

标签 iphone ios uiview uiscrollview uibutton

我需要以编程方式创建一个 UIButton,然后将其放在创建的 UIScrollView 上,然后将 UIScrollView 放在 UIView 上。如果将这些元素添加到 self.view 它们会显示,但是当我想嵌套时它们不会显示。 这是我目前所拥有的:

viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
viewWithPictures.contentSize=CGSizeMake(160*[smallImagesFromGallery count], self.bottomView.frame.size.height);

viewWithPictures.backgroundColor=[UIColor greenColor];
NSLog(@"Number of small images: %i",[smallImagesFromGallery count]); 

for(int i=0; i<[smallImagesFromGallery count]; i++)
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);

    [btn setBackgroundImage:[smallImagesFromGallery objectAtIndex:i] forState:UIControlStateNormal];
    if (btn==nil) {
        NSLog(@"Button is nil");
    }
    btn.tag=i;
    [btn addTarget:self action:@selector(viewLargeVersion:) forControlEvents:UIControlEventTouchUpInside];
    [viewWithPictures addSubview:btn];



}
[bottomView addSubview:viewWithPictures];

最佳答案

当您设置将成为 subview 的 View 的框架时,您需要引用它将添加到的 View 的边界。所以我认为您需要更改几行:

viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];

应该是:

viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.bounds];

btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);

应该是:

btn.frame=CGRectMake(i*160, 0, 150, 100);

关于iphone - 将 UIButton 添加到 UIScrollView,然后将 UIScrollView 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928395/

相关文章:

iphone - 从 iOS 发布到 Rails 应用程序时序列化嵌套对象

ios - 如何在Storyboards中的UITableViewController底部添加工具栏?

swift - WebKitView 不遵守自动布局规则

ios - 如何在拖动另一个按钮时检测 View 中的 UIButton?

ios - UIToolbar 项目未显示在弹出窗口中

objective-c - iPhone iOS 如何通过旋转以编程方式截取屏幕截图?

ios - 您输入的应用名称已被使用

ios - 是否有适用于 iOS 8 的新字体?

iphone - 解析json嵌套对象和数组的组合

iphone - iphone 应用程序中的日期选择器