iphone - 向 UIScrollview 添加 View

标签 iphone ios uiview uiscrollview

我创建了一个 UIView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UIImage *image2 = [UIImage imageNamed:@"pinGreen_v1.png"];
        UIImage *image1 = [UIImage imageNamed:@"PinDown1.png"];

        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
        [btn1 setImage:image2 forState:UIControlStateNormal];
        [self addSubview:btn1]; 

        for(int i =1; i<20; i++){
            UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
            [btn2 setImage:image1 forState:UIControlStateNormal];
            [btn2 addTarget:self 
                     action:@selector(buttonPressed) 
           forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn2];
        }

    }
    return self;
}

使用上面的代码,我正在 View 中绘制图像。我正在从我的 View Controller 加载这个 View 。我需要将此 View 放入 UIScrollview .我应该在哪里创建 ScrollView ?在上述 View 或创建上述 View 的 View Controller 中。

我的 View Controller 内的代码是:
 DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
 drawLines.backgroundColor = [UIColor blackColor];
 [self.view addSubview:drawLines];
 [drawLines release];

我应该能够滚动浏览 View 内的图像。

最佳答案

我会在 View Controller 中创建 ScrollView 并将 View 添加到其中。

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;

DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];

[scroll addSubview:drawLines];

scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];

关于iphone - 向 UIScrollview 添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348406/

相关文章:

iphone - iOS 5.1 的 xcode 4.3 安装

ios - 从 Swift 中的闭包返回多个 View 对象

iOS 以编程方式创建 View

ios - 如果我的 UIViews 边框颜色 alpha 小于 1.0,如何避免颜色重叠?

objective-c - 连续更改 Imageview 图像在 iphone sdk 中给出内存警告

ios - 我如何制作一个 Sequence 来遍历从 UIView 到 View 层次结构的所有祖先( super View )到根?

iphone html css 教程

ios - 关于 AFNetworking 2

ios - tableView.datasource = 自身错误 :unrecognized selector

ios - 当我用 UIView 覆盖所有屏幕时,如何用 UIView 覆盖 UIStatusBar? (iPhone)