繁体   English   中英

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

[英]Is it possible to scroll from UIWebView object to a UIView object?

在我的应用程序的一个用户界面中,我希望我的应用程序显示一个滚动视图(滚动页面的数量在运行时动态设置)。 但是我想将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:方法webViewDidFinishLoad:

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


编辑:查看您的代码,似乎就像您在将UIWebView和两个UIColor对象粘贴到数组中一样。 因此,您需要检查是否正在处理UIWebViewUIColor ,否则将在subview.backgroundColor = [colors objectAtIndex:i];上崩溃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)];

这肯定会滚动视图!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM