簡體   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