是否可以从UIWebView对象滚动到UIView对象



在应用程序的一个用户界面中,我希望应用程序显示滚动视图(滚动页面的数量在运行时动态设置)。但是我想将UIWebView和UIView元素添加到同一个数组中,这样当我滚动时,我就可以同时显示UIWebView(比方说网页)和其他一些View?

编辑

我收到了这个答案,看起来很好,但不起作用(或者我不能)这是代码:NSArray*myArray;//在这里用一堆UIWebView和UIView对象填充数组

for (id theObject in myArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
        // do other stuff
    }
}

有人能帮忙吗?我需要创建一个可滚动的视图数组(像iPhone的主页),但第一个页面(平均值,视图)必须是UIWebView的成员(其他是UIView

下面是我所有的代码(我不想在这里做任何事情:)

  CGRect frame;
     [webView initWithFrame:frame];
    NSArray *colors = [NSArray arrayWithObjects:webView, [UIColor blackColor],[UIColor blackColor],  nil];
        for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [subview setTag:i];
        if([subview tag]==2){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(70.0f, 600.0f, 250.0f, 60);
            [button  setTitle:@"Read Messages" forState:UIControlStateNormal];
                for(NSDictionary* buttonData in butDat) { 
                UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                NSString* buttonTitle = [buttonData objectForKey:@"name"];
                NSString* buttonID=[buttonData objectForKey:@"order_number"];
                [button setTitle:buttonTitle forState:UIControlStateNormal];
                [button setTag:[buttonID intValue]];
                button.titleLabel.font=[UIFont systemFontOfSize:28];
                 CGFloat yPosition = 60.0f;
                const CGFloat buttonHeight = 85.0f;
                if (button.tag==1) {
                    button.frame = CGRectMake(70.0f, yPosition, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];
                }
                if (button.tag==2) {
                    button.frame = CGRectMake(440.0f, yPosition, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];   
                }
                if (button.tag==3) {
                    button.frame = CGRectMake(70.0f, 200.0f, 250.0f, buttonHeight);
                    //[ma.view addSubview:button];
                }
                if (button.tag==4) {
                    button.frame = CGRectMake(440.0f, 200.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****
                }
                if (button.tag==5) {
                    button.frame = CGRectMake(70.0f, 335.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****
                }
                if (button.tag==6) {
                    button.frame = CGRectMake(440.0f, 335.0f, 250.0f, buttonHeight);
                    //[menu_2.view addSubview:button];//****
                }
                [button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:button];
            }
                //

            [button addTarget:self action:@selector(readM:) forControlEvents:UIControlEventTouchUpInside];
          //  [scrollView addSubview:button];
            [self.view addSubview:button];

            //  [self.scrollView addSubview:button];
        }
        [self.scrollView addSubview:subview];
没有什么可以阻止您将类型为UIWebViewUIView的对象添加到同一个NSArray

如果您的代码需要知道您处理的是UIWebView还是UIView,可以使用方法isKindOfClass:进行检查:

NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
        // do other stuff
    }
}

此外,UIWebViewUIView的子类,继承了许多相同的方法,使视图布局在两种情况下都相同。

从用户界面的角度来看,在滚动视图中包含滚动视图可能是一种糟糕的做法,因此您需要禁用UIWebView对象的滚动。如果你的目标是iOS 5及以上版本,你可以使用以下功能:

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;

如果你使用的iOS版本低于5.0,你可以注入以下javascript:

<script type="text/javascript">
    touchMove = function(event) {
        event.preventDefault();
     }
</script>

要实现javascript注入,可以在webViewDidFinishLoad: 中使用UIWebViewstringByEvaluatingJavaScriptFromString:方法

有关UIWebView javascript注入的更多信息,请参阅此处:http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview


编辑:查看您的代码,您似乎在将一个UIWebView和两个UIColor对象粘贴到数组中。因此,您需要检查您处理的是UIWebView还是UIColor,否则您将在subview.backgroundColor = [colors objectAtIndex:i];上崩溃。此外,您还将UIWebView对象的帧设置为未初始化的CGRect frame;

您应该用UIWebView填充NSArray,然后用背景色设置两个UIView对象:

UIWebView *webView = [[UIWebView alloc] init];
UIView *viewOne = [[UIView alloc] init];
[viewOne setBackgroundColor:[UIColor blackColor]];
UIView *viewTwo = [[UIView alloc] init];
[viewTwo setBackgroundColor:[UIColor blackColor]];
NSArray *viewArray = [NSArray arrayWithObjects:webView, viewOne, viewTwo, nil];
for (id theObject in viewArray)
{
    if ([theObject isKindOfClass:[UIWebView class]]) {
        // do stuff
    }
    else if ([theObject isKindOfClass:[UIView class]]) {
       // do other stuff
    }
}

是。UIWebViews是UIViews的派生。您可以在任何将UIView作为子视图插入的地方使用它们,包括在UIScrollView父视图中,或作为要插入数组的对象。

您的滚动视图没有滚动,因为您没有设置contentsize属性。首先你必须找到合适的内容大小,然后你必须将内容大小设置为

[scrollView setContentSize:CGSize];

在代码末尾。即;之后

[self.scrollView addSubview:subview];

对于盲试,请使用

[scrollView setContentSize:CGSizeMake(1000,1000)];

这肯定会滚动视图!!!

最新更新