2011-12-06 25 views
3

我想在我的分組UITableView後添加一個視圖。不幸的是,每當我滾動時,背景視圖也會移動。甚至當我使用[self.view insertSubview:backgroundView belowSubview:_tableView]或[_tableView setBackgroundView:backgroundView]時,會發生這種情況。爲什麼這個背景視圖滾動?此外,爲什麼我的tableView滾動,即使我禁用滾動?這些有關係嗎?如何在UITableView中製作靜態背景視圖?

在應用程序的委託:

History *historyController = [[History alloc] init]; 
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:historyController]; 

在History.m:

- (void)viewDidLoad { 

    CGRect frame = self.view.frame; 
    frame.origin.x = 0; 
    frame.origin.y = 0; 
    _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped]; 
    _tableView.delegate = self; 
    _tableView.dataSource = self; 
    [_tableView setScrollEnabled:NO]; 
    [_tableView setBackgroundColor:[UIColor clearColor]]; 
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    UIView *bg = [[UIView alloc] initWithFrame:frame]; 
    [bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]]; 
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CenterFade.png"]]; 
    iv.alpha = .750; 
    [bg addSubview:iv]; 
    [iv release]; iv = nil; 

    [self.view addSubview:bg]; 
    [self.view addSubview:_tableView]; 
    [bg release]; bg = nil; 
    [_tableView release]; 

    [super viewDidLoad]; 
} 

回答

1

我不知道爲什麼會發生這種情況,但由於某些原因,self.view是UITableView而不是UIView。創建一個新的UIView並將其設置爲self.view修復了這個問題。我沒有使用UITableViewController,而是UIViewController。不知道UITableView來自哪裏!

+0

我最終從模擬器中刪除了應用程序,當我再次運行時,此問題消失了。看起來像是某種沒有更新的錯誤,儘管反覆清理和重建。 (我已經從UITableViewController更改爲UIViewController)。 – arsenius

1

嘗試

- (void)viewDidLoad { 

    CGRect frame = self.view.frame; 
    frame.origin.x = 0; 
    frame.origin.y = 0; 

    UIView *bg = [[UIView alloc] initWithFrame:frame]; 
    [bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]]; 
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CenterFade.png"]]; 
    iv.alpha = .750; 
    [bg addSubview:iv]; 
    [iv release]; 
    iv = nil; 

    _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped]; 
    _tableView.delegate = self; 
    _tableView.dataSource = self; 
    [_tableView setScrollEnabled:NO]; 
    [_tableView setBackgroundColor:[UIColor clearColor]]; 
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    [bg addSubview:_tableView]; 
    [_tableView release]; 
    _tableView = nil; 
    [self.view addSubview:bg]; 
    [bg release]; 
    bg = nil; 

    [super viewDidLoad]; 
} 

如果您的控制器是UITableViewController只是其與tableView更改爲UIViewController<UITableViewDelegate, UITableViewDatasource>財產(這是一樣的)

+0

我完全不爲什麼,但即使加入後才明白這種觀點將不會被移動tableview到背景視圖,背景視圖仍然上下滾動。這讓我瘋狂! – arsenius

1

設置[_tableView setBackgroundColor:[UIColor clearColor]];

,然後把你的背景視圖中的UIViewController視圖層次的UITableView下。

+0

這將無法正常工作,因爲我將背景顏色設置爲模式(儘管我想我可以使用第三個視圖並在其上設置背景圖案)。此外,它已經在UITableView之下。爲什麼你需要在背景上設置一個清晰的背景顏色? – arsenius

+0

您可以將表格背景圖案設置爲任何視圖背景。當你的桌子有透明背景時,你可以自由地在其下面繪製任何視圖和控件。我不明白這個原因,如果需要的話,可以購買。 –

0

UITableView有一個backgroundView屬性。

斯威夫特:

var backgroundView: UIView? 

Objective-C的

@property(nonatomic, readwrite, retain) UIView *backgroundView 

當您滾動的tableView