2013-12-09 27 views
0

我想用靜態imageview覆蓋一個網格視圖單元格,但是當我添加imageview時,它出現在單元格下方,但我希望它位於單元格之上,以便單元格圖像被靜態imageview覆蓋細胞移動時可以移動。在我的代碼中,我的imageview正在打擊單元格,但是我想要一個固定的圖像始終位於gridcell的頂部。當我移動電池我的ImageView不動我的gridcell重疊我的imageview

DWExampleGridViewController.h 
#import "DWGridController.h" 

@interface DWExampleGridViewController : DWGridViewController 
{ 

} 
@property (strong, nonatomic) IBOutlet UIImageView *iv; 
@end 


    DWExampleGridViewController.m 


    #import "DWExampleGridViewController.h" 
    #import "DWExampleGridViewCell.h" 
    #import "DWExampleGridViewController.h" 
    #import "DWGridViewController.h" 
    @interface DWExampleGridViewController() 

    @end 

    @implementation DWExampleGridViewController 
    @synthesize iv; 
    - (id)init 
    { 
     self = [super init]; 
     if (self) 
     { 

      for(int row = 0; row < 9; row++) 
      { 
       for(int col = 0; col < 9; col++) 
       { 

         DWExampleGridViewCell *cell = [[DWExampleGridViewCell alloc] init]; 
         UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d-%d.jpeg",row,col]]; 

         iv = [[UIImageView alloc] initWithImage:image]; 
         [iv setContentMode:UIViewContentModeScaleAspectFill]; 
        iv.clipsToBounds = YES; 

         [iv setTranslatesAutoresizingMaskIntoConstraints:NO]; 
         //[cell addSubview:iv]; 
        //[self.view insertSubview:iv atIndex:0]; 

         [cell insertSubview:iv atIndex:0]; 

         [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[iv]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iv)]]; 
         [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[iv]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iv)]]; 

         NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
         [dict setObject:cell forKey:@"Cell"]; 
         [dict setObject:[NSNumber numberWithInt:row] forKey:@"Row"]; 
         [dict setObject:[NSNumber numberWithInt:col] forKey:@"Column"]; 


         if(image) 
          [dict setObject:image forKey:@"Image"]; 
         [self.cells addObject:dict]; 
       } 
      } 


     } 
     [self image]; 

     return self; 
    } 

    - (void)viewDidLoad 
    { 


    } 
    -(void)image 
    { 
     DWExampleGridViewCell *cell = [[DWExampleGridViewCell alloc] init]; 
     UIImageView *v = [[UIImageView alloc] init]; 
     v.frame=CGRectMake(108,155, 105, 160); 
     v.image=[UIImage imageNamed:@"4.jpeg"]; 
     [v setContentMode:UIViewContentModeScaleAspectFill]; 
     v.clipsToBounds = YES; 
     //DWExampleGridViewController *grid = [[DWExampleGridViewController alloc]init]; 
     //[self.view insertSubview:v aboveSubview:grid.iv]; 
     //[self.view insertSubview:v atIndex:2]; 
     //[cell insertSubview:v atIndex:1]; 
     //[cell insertSubview:v aboveSubview:iv]; 
     [[[[UIApplication sharedApplication] delegate] window] insertSubview:v aboveSubview:cell]; 
     [self.view bringSubviewToFront:self.gridView]; 

    } 



    #pragma mark - GridView datasource 
    -(NSInteger)numberOfColumnsInGridView:(DWGridView *)gridView 
    { 
     return 8; 
    } 

    -(NSInteger)numberOfRowsInGridView:(DWGridView *)gridView 
    { 
     return 8; 
    } 

    -(NSInteger)numberOfVisibleRowsInGridView:(DWGridView *)gridView 
    { 
     return 3; 
    } 

    -(NSInteger)numberOfVisibleColumnsInGridView:(DWGridView *)gridView 
    { 
     return 3; 
    } 


    #pragma mark - GridView delegate 
    -(void)gridView:(DWGridView *)gridView didSelectCell:(DWGridViewCell *)cell atPosition:(DWPosition)position 
    { 
     NSDictionary *cellDictionary = [self cellDictionaryAtPosition:position]; 
     UIImage *image = [cellDictionary objectForKey:@"Image"]; 

     UIButton *button = [[UIButton alloc] init]; 
     button.translatesAutoresizingMaskIntoConstraints = NO; 
     [button setBackgroundImage:image forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchDown]; 

     UIViewController *contr = [[UIViewController alloc] init]; 
     [contr.view addSubview:button]; 

     [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 
     [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 

     [self presentViewController:contr animated:YES completion:nil]; 
    } 

    -(void)buttonTapped 
    { 

     [[self presentedViewController] dismissViewControllerAnimated:YES completion:nil]; 
    } 
    @end 

回答

0
  1. 做一個自定義單元格
  2. 靜態圖像添加到單元格內容查看使用addSubview:
  3. 每當你想要的形象在最前面通話[cell.contentView bringSubviewToFront:imageView]

如果這沒有幫助 - 張貼什麼是錯誤的和你想要它的截圖。

0
  1. 使視圖具有與單元格相同的屬性。
  2. 在所需位置上的keywindow上添加該視圖。
  3. 永遠不會忘記[view removeFromSuperview] on viewWillDisappear delegate。