我想用静态图像视图覆盖网格视图的一个单元格但是当我添加图像视图时,它会出现在单元格下方,但我希望它在单元格上方,以便细胞图像被静态图像视图覆盖,静态图像视图可以在细胞移动时移动。在我的代码中,我的imageview正在吹动单元格,但我想要一个固定的图像始终只是网格单元的顶部。当我移动单元格时,我的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 :(得分:0)
addSubview:
[cell.contentView bringSubviewToFront:imageView]
如果这没有帮助 - 发布错误的屏幕截图以及您希望它如何。
答案 1 :(得分:0)