[英]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.