[英]Loading custom UIView in UIViewController's main view
我已經繼承了UIView的子類,並創建了一個NIB來控制我的應用程序的主要邏輯。
希望視圖可以很好地縮放,我想將其用於該應用程序的iPhone和iPad版本。
在iPhone上,視圖將覆蓋全屏。 在iPad上,視圖僅覆蓋部分屏幕。
我讀過您不應該使用UIViewControllers來僅控制屏幕的一部分。 因此,我試圖使用IB將自定義UIView嵌入到主UIViewController的視圖中。
如何才能做到這一點?
經過大量的試驗和錯誤,我找到了一個解決方案,該解決方案是由以下問題解釋的,由Brian Webster回答。
該解決方案最初是針對可可環境提出的。 我希望它在iOS環境中也有效。
這是一些代碼:
MainViewController.h
@interface MainViewController : UIViewController { CustomViewController *customViewController; UIView *customView; } @property (nonatomic, retain) CustomViewController *customViewController; @property (nonatomic, retain) IBOutlet UIView *customView; @end
MainViewController.m
- (void)viewDidLoad { CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil]; self.customViewController = controller; [controller release]; customViewController.view.frame = customView.frame; customViewController.view.autoresizingMask = customView.autoresizingMask; [customView removeFromSuperview]; [self.view addSubview:customViewController.view]; self.customView = customViewController.view; [super viewDidLoad]; }
IBOutlet
屬性添加到UIViewController,並為要訪問的任何子視圖添加其他出口。 -
[[NSBundle mainBundle] loadNibNamed:@"MyNib" owner:self options:0];
self.myCustomView.frame=self.view.bounds; // make view fill screen - customize as necessary
[self.view addSubview:self.myCustomView];
加載NIB時,將在步驟1中設置的插座中填充從NIB加載的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.