2011-01-09 76 views
46

我很難從一個UITableViewController的viewDidLoad方法中如何添加當前的UITableViewController上面一個UIView

這個工程添加子視圖(UIView的):

[self.view addSubview:self.progView]; 

但是你可以看到表格單元格行通過UIView progView流血。

我試過這種方法:

[self.view.superview insertSubview:self.progView aboveSubview:self.view]; 

這是試圖將progView,UIView的增加了上海華,當前視圖上面。當我嘗試這個時,UIView從不出現。

- 更新 -

以下是最新的嘗試:

UIView *myProgView = (UIView *)self.progView; //progView is a method that returns a UIView 

[self.tableView insertSubview:myProgView aboveSubview:self.tableView]; 
[self.tableView bringSubviewToFront:myProgView]; 

結果是一樣的[self.view addSubview:self.progView]。 UIView出現,但似乎在桌子後面。

+2

UITableView的頂部,你是否嘗試過使用`aboveSubview:self.view`?我不是說它會解決你的問題,但它聽起來像會擺脫你所得到的錯誤信息。 – 2011-01-09 21:25:32

+0

是的,我確實嘗試過。誠然,我失去了警告,但它仍然不呈現視圖。我會編輯我的帖子。 – user558096 2011-01-10 00:48:33

+0

一個簡單的方法是設置progVIew的框架,我相信哪個可以工作。任何約束都不起作用,包括VFL – BollMose 2015-05-28 07:54:30

回答

1

嘗試:[self.view bringSubviewToFront:self.progView];

或者你可以嘗試添加self.progView你的表的觀點。

+0

[self.view bringSubViewToFront:self.progView];給我的警告:'UIView'可能不會響應'-bringSubViewToFront:'並使用「無法識別的選擇器發送到實例」異常來崩潰應用程序。 – user558096 2011-01-10 01:02:09

+0

那麼你的控制器是一個UITableViewController? – WrightsCS 2011-01-10 01:24:08

+0

是的,我的控制器是一個UITableViewController。 – user558096 2011-01-10 01:30:11

6

問題是UITableViewControllerview屬性與tableView屬性相同。這意味着根視圖是總是一個表視圖控制器,作爲子視圖添加的任何東西都將受到表視圖功能的支配。這還有其他不良的副作用,比如你的子視圖在你不想要的時候滾動。

這裏有幾個選項。您可以覆蓋loadView和安裝自己的看法和表格視圖:

// note: untested 
- (void)loadView { 
    self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
    self.view.backgroundColor = [UIColor whiteColor]; 

    UITableView *tblView = [[UITableView alloc] 
     initWithFrame:CGRectZero 
     style:UITableViewStylePlain 
    ]; 

    tblView.autoresizingMask = 
     UIViewAutoresizingFlexibleWidth | 
     UIViewAutoresizingFlexibleHeight 
    ; 

    self.tableView = tblView; 
    [self.view addSubview:tblView]; 
    [tblView release]; 
} 

然後當你需要添加一個子視圖,添加它低於或高於適當self.tableView

另一種選擇是創建一個UIViewController子類,它可以滿足您的需求。 UITableViewController坦率地說不會增加太多,並且它實現的小功能很容易被複制。有像Recreating UITableViewController to increase code reuse這樣的文章可以很容易地解釋如何做到這一點。

+0

感謝您的幫助。不幸的是,我真的很努力使你的第一個建議工作。也許我想要做的只是不可能的。在這一點上,我試圖讓UIView成爲自我。UITableViewController子類的視圖。然後使UITableView成爲UIView的子視圖。它似乎不可能做到這一點,我真的需要根據您的第二個建議將我的UITableViewController轉換爲UIViewController。任何想法或指向教程或示例代碼可能會有所幫助。提前致謝。 – user558096 2011-01-13 17:26:38

+1

我不會引用self.view在loadView中讀取,最終會以無限循環結束。只分配給它。 – Diziet 2011-07-25 17:07:21

5

Apple示例「iPhoneCoreDataRecipes」使用NIB將標題加載到UITableView。 請參閱here

+0

我同意這個答案! – Jagie 2017-04-11 14:50:00

11

我已經能夠通過使用uiviewcontroller包容在uitableviewcontroller的頂部添加子視圖。

當談到靜態單元時,UITableViewController實際上非常方便,這可能是唯一一次常見問題「使用uitableview」可能實際上不可行。

所以這就是我的做法。

  1. 給你的UITableViewController一個StoryBoard標識符即MyStaticTableView
  2. 創建一個全新的的UIViewController子類,並將其命名爲UITableViewControllerContainer
  3. 地方這個控制器代替你的UITableViewController的故事板內
  4. 一個子視圖添加到新的控制器,並將其鏈接到一個叫做「view_container」的插座
  5. 就在你身上UITableViewControllerContainer viewDidLoad方法

添加如下代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    UITableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyStaticTableView"]; 
    [self addChildViewController:vc]; 
    [self.view_container addSubview:vc.view]; 
} 

問題,你可能有:

  1. ,如果你有多餘的頂部空間,那麼就一定要添加標誌「想全屏」給你UITableViewController
  2. 如果它沒有在您的UITableViewControllerContainer上正確調整大小

添加代碼:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [[self.view_container.subviews lastObject] setFrame:self.view.frame]; 
} 

從您的UITableViewController這一點,你可以用

self.view.superview.superview

,無論您添加直接訪問你的容器視圖它會顯示在你的桌面視圖控制器上面

87

我試了一下上面的方法,但沒有得到它的工作。我還發現它需要太多的配置和代碼,因爲它需要從頭開始設置表格視圖(從故事板內容易完成的事情)。

相反,我還說,我想補充我上面的UITableView到的UITableViewController的的UINavigationController的觀點來看,這樣:

[self.navigationController.view addSubview:<view to add above the table view>]; 

這種方法需要您已經嵌入在一個UINavigationController的的UITableViewController,但即使你不想要一個導航控制器,你仍然可以使用這種方法並隱藏導航欄。

28

這實際上是非常可能和簡單 - 它只需要一點點重新佈線。我遇到了類似的情況,並對其進行了編碼,以允許我在桌面視圖頂部添加加載屏幕。這種方法還允許您將靜態表格單元格用於您自己的內容。

如果您的控制器是故事板或XIB中的UITableViewController,並且您希望重新指定自我。以一個標準的UIView,同時保留現有的表視圖:

添加這些實例變量您頭文件

IBOutlet UITableView *tableViewReference; // to keep a reference to the tableview 
UIView *viewReference; // a reference to the new background view 

在你故事板或XIB文件:UITableViewController連接tableView到變量tableViewReference

然後在您實現文件

// Override the default accessors of the tableview and view 
- (UITableView*)tableView { return tableViewReference; } 
- (UIView*)view { return viewReference; } 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // instantiate the new self.view, similar to the tableview 
    viewReference = [[UIView alloc] initWithFrame:tableViewReference.frame]; 
    [viewReference setBackgroundColor:tableViewReference.backgroundColor]; 
    viewReference.autoresizingMask = tableViewReference.autoresizingMask; 
    // add it as a subview 
    [viewReference addSubview:tableViewReference]; 

    /* remainder of viewDidLoad */ 

} 

雖然它可能看上去很奇怪,是的UITableViewController做了很多離奇的東西與幕後的引用。您需要等到viewDidLoad才能進行交換。在此之後,訪問器將指向正確的UIViewUITableView

用法: 喜歡的東西

[self.view addSubview:myView]; 

將添加一個UIView myView表視圖的前面,而不myView與表視圖的內容滾動。

注意:這個答案在被指出該解決方案包含邊界矛盾代碼後進行了編輯。 UITableViewController對它所持有的引用做了一些奇怪的事情。一些更仔細的測試得出這個更簡單的答案:)

4

我在表格上方添加了一個圖像。我用這個代碼:

self.tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]]; 
0

我有一個類似的問題,我想在我的UITableViewController頂部添加一個加載指標。爲了解決這個問題,我添加了我的UIView作爲窗口的子視圖。這解決了問題。這是我做到的。

-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    //get the app delegate 
    XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 

    //define the position of the rect based on the screen bounds 
    CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50); 
    //create the custom view. The custom view is a property of the VIewController 
    self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect]; 
    //use the delegate's window object to add the custom view on top of the view controller 
    [delegate.window addSubview: loadingView]; 
} 
0

有可能是有理由不這樣做,但這對我來說迄今爲止。如果您使用的AP

裏面viewDidLayoutSubviews你可以運行它,但請務必只運行一次明顯

self.searchTableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStylePlain]; 
self.searchTableView.backgroundColor = [UIColor purpleColor]; 
[self.view.superview addSubview:self.searchTableView]; 
8

您可以增加您的視圖層的z位置。

這將使上面顯示的其他視圖(其中有等於0的z位置,默認情況下)

self.progView.layer.zPosition++; 
[self.view addSubview:self.progView]; 
0

嘗試這樣的事:

[self.tableView addSubview:overlayView]; 
overlayView.layer.zPosition = self.tableView.backgroundView.layer.zPosition + 1; 
2

我有類似的問題,得到了它解決了使用下面的代碼:

 [self.navigationController.view insertSubview:<subview> 
     belowSubview:self.navigationController.navigationBar]; 

這使用存在於堆棧中的控制器插入視圖在正確的位置。

2

由於UITableViewController是UIViewController的子類,因此您需要將所需的視圖添加到其超級視圖。

Swift: 
self.view.superview?.addSubview(viewTobeAdded) 

Objective C: 
[self.view.superview addSubview: viewTobeAdded]; 
0

您可以簡單地把下面的代碼在viewDidAppear:

[self.tableView.superview addSubview:<your header view>]; 
1

解決方案迅速(相當於丹尼爾·賽義迪):

如果控制器是在故事板或XIB一個UITableViewController和您希望將self.view重新分配給標準UIView,同時保留您現有的表格視圖:

@IBOutlet var tableViewReference: UITableView! 
var viewReference: UIView! 

然後在您的實現文件:

添加這些實例變量到你的表視圖控制器文件:

override var tableView: UITableView! { 
    get { return tableViewReference } 
    set { super.tableView = newValue } 
} 

override var view: UIView! { 
    get { return viewReference } 
    set { super.view = newValue } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.edgesForExtendedLayout = UIRectEdge.None 
    self.extendedLayoutIncludesOpaqueBars = false 
    self.automaticallyAdjustsScrollViewInsets = false 

    //rewiring views due to add tableView as subview to superview 
    viewReference = UIView.init(frame: tableViewReference.frame) 
    viewReference.backgroundColor = tableViewReference.backgroundColor 
    viewReference.autoresizingMask = tableViewReference.autoresizingMask 
    viewReference.addSubview(tableViewReference) 
} 

在你的故事板或XIB文件:在的UITableViewController連接的tableView到tableViewReference變量。

然後你就可以添加子發表意見如下:

self.view.addSubView(someView) 
1

爲了保持上述的UIView表視圖中的UITableViewController我使用的委託方法(的UITableViewDelegate)一個(或多個)。

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.addSubview(headerView) 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    tableView.addSubview(overlayView) // adds view always on top 
} 
// if using footers in table view 
tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { ... } 

由於意見只能有一個超級查看接縫也是很好的解決方案,糾正我,如果我錯了。仍然獲得60fps,所以對我來說很好。

0

更新爲iOS 11:

現在可以使用自動版式限制到安全區時,一個子視圖添加到UITableView。這些視圖不會沿着TableView滾動。

這個例子會將導航欄下方的視圖上的UITableViewController

[self.tableView addSubview:self.topBarView]; 
[NSLayoutConstraint activateConstraints:@[ 
    [self.topBarView.topAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.topAnchor], 
    [self.topBarView.leadingAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.leadingAnchor], 
    [self.topBarView.trailingAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.trailingAnchor], 
    [self.topBarView.heightAnchor constraintEqualToConstant:40.0] 
]]; 
相關問題