【问题标题】:Add A UIButton At The End Of A View With Dynamic Height在具有动态高度的视图末尾添加一个 UIButton
【发布时间】:2013-10-30 10:17:23
【问题描述】:

我有一个UIView 包含UIWebView 和一个UILabel. 这个UIView 被添加到UIScrollView. 中因为UIWebView 具有动态高度,所以UIScrollView 相应地改变它的高度。一切都很好,一切都在故事板中完成。

现在我需要在UIWebView 的末尾或之后添加一个UIButton。我不能只是在 StoryBoard 中拖放一个按钮,因为它保持在固定位置。

我想要的是当滚动结束时,底部应该出现UIButton。我怎样才能做到这一点?

【问题讨论】:

    标签: ios iphone objective-c uibutton


    【解决方案1】:

    按钮 y 位置将为 Label_Height+Webview_Height+(Margin_between_webview_and_button) 所以,相应的scrollview内容高度就是Label_Height+WebView_Height+Button_Height

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"This is a button" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, height, 160.0, 40.0);
    [self.webView addSubview:button];
    

    在上述代码的倒数第二行中,“height”是您已经检索到的 webview 的高度。只需添加 webview 的高度并将按钮添加为 webview 的子视图即可。

    【讨论】:

      【解决方案2】:

      如果您想在UIWebView 的底部添加UIButton,而不是像下面这样更改您的按钮框架。

       `button.frame.origin.y = webview.frame.origin.y + webview.frame.size.height;`
      
       Implement the above line after you get the finally webview height dynamically and set `UIScrollView` `contentsize` accordingly.
      
       `[scrollView setContentSize:CGSizeMake(scrollview.frame.size.width,button.frame.origin.y + button.frame.size.height )];`
      

      【讨论】:

        【解决方案3】:

        您可以使用 stringByEvaluatingJavaScriptFromString 方法将 <a href='myTag01'> <button>Button </button> </a> 之类的 html 注入到 webview 的内容中。

        要捕获点击事件并防止它重新加载您的 webview 的内容,请使用此委托:

        - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
        {
            if(navigationType==UIWebViewNavigationTypeLinkClicked && [[request.URL absoluteString] isEqualToString: @"yourTag01"])
            {
                //your custom action code goes here
                return NO;
            }
            return YES;
        }
        

        【讨论】:

          【解决方案4】:

          尝试将 UIButton 拖到 UIScrollView 并将其在 xib 中的自动调整大小掩码设置为 BOTTOM 只需检查标记底线,这应该可以工作,删除框内的其他标记,并在底部保持一条红线处于活动状态。这应该有效。看看吧

          【讨论】:

          • 已经尝试过了,但它会随着滚动视图滚动,即使未加载 webview,它也会显示按钮。我想要 UIWebView 内容结束的按钮。
          • 那么您需要以编程方式执行此操作。这是你能做到的唯一方法
          【解决方案5】:

          首先像这样将 UIScrollView 委托添加到你的头文件中

          @interface ScrollView : UIScrollView <UIScrollViewDelegate>
          

          然后在你的实现文件中添加这个委托方法:

          -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
               if (scrollView.contentOffset.y >= scrollView.contentSize.width) {
                   // Add a UIButton
               }
          
          }
          

          【讨论】:

          • 执行您提到的代码不会发生任何事情。尝试在不同的视图中添加按钮,但仍然没有显示任何内容
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-30
          • 2020-11-15
          • 1970-01-01
          • 2018-12-21
          • 2011-01-28
          • 1970-01-01
          相关资源
          最近更新 更多