【问题标题】:Is it possible to scroll from UIWebView object to a UIView object?是否可以从 UIWebView 对象滚动到 UIView 对象?
【发布时间】:2012-02-22 00:33:56
【问题描述】:

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

编辑

我收到了这个看起来完全正常但不起作用的答案(或者我不能) 这是代码: 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];

【问题讨论】:

    标签: iphone uiwebview uiscrollview uipagecontrol


    【解决方案1】:

    没有什么能阻止您将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;
    

    如果您使用的是低于 5.0 的 iOS 版本,则可以注入以下 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
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        [scrollView setContentSize:CGSize];
        

        在代码的末尾。 IE;之后

        [self.scrollView addSubview:subview];
        

        如果是盲目尝试,请使用

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

        这肯定会滚动视图!!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多