在具有动态高度的视图的末尾添加UIButton

时间:2013-10-30 10:17:24

标签: ios iphone objective-c uibutton

我有UIView包含UIWebViewUILabel.UIView已添加到UIScrollView.内,因为UIWebView具有动态高度,所以UIScrollView会相应地改变它的高度。整个过程都很好,一切都在故事板中完成。

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

我想要的是当滚动结束时,底部会出现UIButton。我怎样才能实现这一目标?

UIVIEW UIButton

5 个答案:

答案 0 :(得分: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的子视图即可。

答案 1 :(得分:1)

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

 `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 )];`

答案 2 :(得分:0)

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

要捕获click事件并阻止它重新加载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;
}

答案 3 :(得分:0)

尝试将UIButton拖动到UIScrollView并将其自动调整大小的掩码设置为BOTTOM 只需检查标记底线,这应该工作,删除框内的其他标记,只是在底部激活一条红线。这应该工作。看看

enter image description here

答案 4 :(得分:0)

首先将UIScrollView委托添加到您的头文件中,如下所示

@interface ScrollView : UIScrollView <UIScrollViewDelegate>

然后在您的实现文件中添加此委托方法:

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

}