[英]Adding an UIView programmatically not working (UIView should show an UIWebView and open URL)
目前正在为我的iPhone应用程序(如Pocket,为用户提供一些动手操作)在两个简介页上(第一个显示图片,第二个应该显示网站)。 遇到了Matthew York的GitHub
项目,该项目可以完美地处理images
和text
。 另外,我希望在介绍中展示一个网站,但无法正常运行。 我以programmatically
创建的UIWebView
并未显示。
Matthew York的GitHub
项目: https : //github.com/MatthewYork/iPhone-IntroductionTutorial
用于显示两个简介页面的完整代码如下所示。 请注意, panelImage
可以正常工作,但是panelView
不能panelView
。 我看到第二页显示了,但是没有UIWebView
。 我想我的添加subview
的view
中不可见的屏幕上,因此我没有看到它。 我对吗? 您能否看一下: [view addSubview:aWebView];
在method
: showWebView
ViewController.m
- (void)showIntro
{
MYIntroductionPanel *panelImage = [[MYIntroductionPanel alloc] initWithimage:[UIImage imageNamed:@"img.jpg"] description:@"TEST: IMAGE"];
MYIntroductionPanel *panelView = [[MYIntroductionPanel alloc] initWitView:[self showWebView] description:@"TEST: VIEW"];
MYIntroductionView *introductionView = [[MYIntroductionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) headerText:@"TESTING" panels:@[panelImage, panelView] languageDirection:MYLanguageDirectionLeftToRight];
introductionView.BackgroundImageView.image = [UIImage imageNamed:@"BG_iPad_1024.png"];
introductionView.delegate = self;
[introductionView showInView:self.view];
}
- (UIView *)showWebView
{
UIWebView *aWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[view addSubview:aWebView];
return view;
}
MYIntroductionPanel.m
-(id)initWitView:(UIView *)view description:(NSString *)description{
if (self = [super init]) {
self.view = [[UIView alloc] init];
self.Description = [[NSString alloc] initWithString:description];
}
return self;
}
-(id)initWithimage:(UIImage *)image title:(NSString *)title description:(NSString *)description{
if (self = [super init]) {
self.Image = [[UIImage alloc] init];
self.Image = image;
self.Title = title;
self.Description = [[NSString alloc] initWithString:description];
}
return self;
}
您要向其添加UIWebView
的视图永远不会添加到视图层次结构本身。 您似乎正在将UIWebView的父视图传递给MyIntroductionPanel的initWithView:description:
方法,但是该方法只是忽略其输入视图,并使用self.view = [[UIView alloc] init]
创建一个新视图。 尝试在initWithView:description:方法中设置self.view = view
。
我不喜欢作者的所有决定,但遵循他的模式来实现您想要的。
添加了以下内容:
// MyIntroductionPanel.h
@property (nonatomic, retain) NSURL *url;
-(id)initWithURL:(NSURL *)url;
// MyIntroductionPanel.m
-(id)initWithURL:(NSURL *)url {
if (self = [super init]) {
_url = url;
}
return self;
}
那很简单。 这不太容易-破解布局方法以处理带有URL的面板。 我并没有尽力去理解这种方法,只是添加了我可以使其工作的一切。 我的添加项标记为// DANH
// MYIntroductionView.m
-(UIView *)PanelViewForPanel:(MYIntroductionPanel *)panel atXIndex:(CGFloat*)xIndex{
// snipped original code
NSInteger descriptionHeight = panelDescriptionTextView.contentSize.height;
int contentWrappedScrollViewHeight = 0;
if ((imageHeight + descriptionHeight + panelTitleLabelFrame.size.height) > maxScrollViewHeight) {
contentWrappedScrollViewHeight = maxScrollViewHeight;
imageHeight = contentWrappedScrollViewHeight-descriptionHeight - panelTitleLabelFrame.size.height - 10;
}
else if ((imageHeight+descriptionHeight + panelTitleLabelFrame.size.height) <= maxScrollViewHeight){
contentWrappedScrollViewHeight = imageHeight + panelTitleLabelFrame.size.height + descriptionHeight;
}
// DANH
if (panel.url) {
contentWrappedScrollViewHeight = 300;
// you can certainly do a better job than I did here, but just to get going
}
panelView.frame = CGRectMake(*xIndex, 0, self.ContentScrollView.frame.size.width, contentWrappedScrollViewHeight);
//Build image container
UIImageView *panelImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, self.ContentScrollView.frame.size.width - 10, imageHeight)];
panelImageView.contentMode = UIViewContentModeScaleAspectFit;
panelImageView.backgroundColor = [UIColor clearColor];
panelImageView.image = panel.Image;
panelImageView.layer.cornerRadius = 3;
panelImageView.clipsToBounds = YES;
[panelView addSubview:panelImageView];
// DANH
// add webview on top if we have a url
if (panel.url) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.ContentScrollView.frame.size.width, 300)];
[webView loadRequest:[NSURLRequest requestWithURL:panel.url]];
[panelView addSubview:webView];
}
// snipped original code
}
现在,用示例代码来调用它。
// MYViewController.m
MYIntroductionPanel *panel3 = [[MYIntroductionPanel alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
// ...
MYIntroductionView *introductionView = [[MYIntroductionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) headerText:@"MYIntroductionView" panels:@[panel, panel2, panel3] languageDirection:MYLanguageDirectionLeftToRight];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.