UIImageView under UITableView does not show - ios

I want to add a UIImageView under a UITableView, using the following code in viewDidLoad:
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.scrollEnabled = NO;
tableView.allowsSelection = NO;
tableView.dataSource = self;
tableView.delegate = self;
tableView.backgroundColor = [UIColor clearColor];
tableView;
});
self.waterMarkImageView = ({
UIImageView *imageView = [UIImageView new];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.alpha = 1.0;
imageView.image = [UIImage imageNamed:#"waterMark"];
imageView;
});
[self.view addSubview:self.waterMarkImageView];
[self.view addSubview:self.tableView];
[self.waterMarkImageView mas_makeConstraints:^(MASConstraintMaker *make){
make.right.equalTo(self.view.mas_right);
make.bottom.equalTo(self.view.mas_bottom);
make.width.equalTo(self.view.mas_width);
make.height.equalTo(self.view.mas_height);
}];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(statusBarHeight + navigationBarHeight + labelHeight);
make.left.equalTo(self.view.mas_left);
make.bottom.equalTo(self.view.mas_bottom);
make.right.equalTo(self.view.mas_right);
}];
However there is no waterMarkImageView shows.
But if I exchange the order that I add waterMarkImageView and tableView to the subviews of self.view, that is:
[self.view addSubview:self.tableView];
[self.view addSubview:self.waterMarkImageView];
Then the waterMarkImageView will show, but above the tableview, that will cover a part of the tableview and that isn't what I expect. Anyone knows how to deal with this problem?
Well sorry actually from the view hierarchy I can see a part of the watermark, not the whole. And when I run the app I can see nothing.

You cannot see the imageView because it is behind the tableView. This makes sense as you added the tableView after the imageView. To fix this, simply set the background color of the cells to clearColor(). This will make them transparent so that you can see the imageView that is behind them.

Related

uitableviewcell get offset after set navigationBarHidden = YES and and a custom bar view

THE BUG PICTURE
This picture is the bug picture,and my code is as follow:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
naviview = [[UIImageView alloc] init];
naviview.backgroundColor = [UIColor colorWithHexString:#"fcfcfc"];
naviview.frame = CGRectMake(0, 0, VIEW_BOUNDS.width, 64);
[self.view addSubview:naviview];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, VIEW_BOUNDS.width, self.view.bounds.size.height-62-TABBARHEIGHT) style:UITableViewStylePlain];
self.tableView.showsHorizontalScrollIndicator = NO;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.backgroundColor = [UIColor grayColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view insertSubview:_tableView belowSubview:self.naviview];
}
if I use [self.view insertSubview:_tableView belowSubview:self.naviview]; the cell have a offset, buf if i use [self.view addSubview:_tableView] the cell will be right. I want to know the reason lead to the offset,someone can tell me?
Thanks a lot!
Because when you use addSubview then it is getting added on superview and the and frame of tableview will be calculated with view's frame. But in insertSubView is getting added behind navView. There might be possibility that tableView frame is getting calculated with different frame.
You can verify it by making tableView frame height 0 and then try using insertSubView.

Scroll Doesn't work Help would be graet

I have tried this code but scrollview is not working.
Please help
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.showsVerticalScrollIndicator=YES;
CGRect contentRect = CGRectZero;
for (UIView *view in scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
scrollView.contentSize = contentRect.size;
//scrollView.contentSize=CGSizeMake(self.view.bounds.size.width,tableView.bounds.size.height+100);
[self.view addSubview:scrollView];
UITableView *tableView=[[UiTableView alloc]initWithFrame:CGRectMake(0,200, self.view.bounds.size.width, self.view.bounds.size.height)];
tableView.dataSource=self;
tableView.delegate=self;
//tableView.keepOneSectionOpen = YES;
//tableView.initialOpenSections = [NSSet setWithObjects:#(0), nil];
tableView.scrollEnabled = NO;
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuseIdentifier];
[tableView registerNib:[UINib nibWithNibName:#"AccordionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:kAccordionHeaderViewReuseIdentifier];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,200)];
imageView.image=[UIImage imageNamed:#"download (1).jpeg"];
imageView.userInteractionEnabled=YES;
imageView.alpha=1;
[scrollView addSubview:imageView];
[scrollView addSubview:tableView];
UITableView is a subclass of UIScrollView, not a subview, so you should just use the table view directly.
For more information read this explanation
I think the way you did is not correct. If you want to add an image above table view, try using tableHeaderView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
self.tableView.tableHeaderView = imageView;

Cannot prevent UITableView from moving when bounds change

I have an UITableView created using this method (inside a UIViewController):
- (void) viewDidLayoutSubviews {
if (!self.tableView) {
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height-120) style:UITableViewStylePlain];
self.tableView.superview.autoresizingMask= NO;
// [self.tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor blueColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
[self.tableView setShowsHorizontalScrollIndicator:NO];
[self.tableView setShowsVerticalScrollIndicator:NO];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
}
}
When the application enters the background and the view controller is then reloaded, the tableView moves lower as a result (I think) of the bounds changing. However, I have tried turning auto layout off in storyboard, turning the autoresizesSubviews "BOOL" of self.view and of self.tableView and have even tried changing the constraints on the tableView. Every time the view loads, the content moves. Furthermore, I've put the initiation into viewDidLoad, but then the actual sizes don't map to the screen. How do I force the tableView to stay fixed after initiating it in viewDidLayoutSubviews?
viewDidLoad is called only once in the view controller lifecycle, and is called before the view has it's bounds property set. This is the perfect place to initialize your subviews.
-(void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height-120) style:UITableViewStylePlain];
self.tableView.superview.autoresizingMask= NO;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor blueColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
[self.tableView setShowsHorizontalScrollIndicator:NO];
[self.tableView setShowsVerticalScrollIndicator:NO];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
}
viewDidLayoutSubviews is called every time the bounds change, so setting the frame in that method will ensure that your tableView is always sized according to the current bounds:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.tableView.frame = CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height-120);
}

iPad - Autoresizing for UITableView

My Code is Here :
CGRect mainFrame = [UIScreen mainScreen].bounds;
_contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0.0, mainFrame.size.width, mainFrame.size.height-20)] autorelease];
_contentView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
_contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_contentView];
self.tableView = [[[UITableView alloc] initWithFrame:_contentView.frame style:UITableViewStylePlain] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.tableView setSeparatorColor:[UIColor clearColor]];
[_contentView addSubview:_tableView];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
I add a tableview on _contentView and _contentView on self.view
on iPhone works good without problem, but on iPad I don't know why when I set autoresizing mask for table view, it increases width and height... without autoresizing mask works good, but I need autoresizing mask for landscape. How to solve this ?
with autoresizing mask
without autoresizing mask
Once try this
_contentView.autoresizesSubviews = YES ;
Hope this will work.

Ipad tableview and imageview

How to insert four tableview in a scrollview by pagination.i know how to do this ,but i need a image view as first page and then tableview as second,third and forth page.
Thankz in advance
_scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 55, 1000, 460)];
_scroll.backgroundColor=[UIColor clearColor];
_scroll.pagingEnabled=YES;
_scroll.contentSize = CGSizeMake(1000*5, 460);
[_scroll setBackgroundColor:[UIColor lightGrayColor]];
CGFloat x=0;
for(int i=1;i<6;i++)
{
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(x+10, 10, 750, 440) style:UITableViewStylePlain];
[table setBackgroundColor:[UIColor whiteColor]];
table.rowHeight = 120;
table.separatorColor = [UIColor grayColor];
table.delegate=self;
//table.dataSource=self;
[_scroll addSubview:table];
[table release];
x+=1000;
}
[self.view addSubview:_scroll];
_scroll.showsHorizontalScrollIndicator=NO;
[_scroll release];
Ignoring the recommendation that UITableViews are not embedded inside scroll views as they are scrollviews in themself. So dont do this (not a joke).
You add the imageview with addSubview: just like the tableviews you want add. They are all UIViews and are all treated the same way.
So for a hypothetical scrollview with size (100,100) and imageview/tableviews of size (100,100)
imageview.frame = CGRectMake(0,0,100,100);
[scrollview addSubview:imageview];
tableview1.frame = CGRectMake(100,0,100,100);
[scrollview addSubview:tableview1];
tableview2.frame = CGRectMake(200,0,100,100);
[scrollview addSubview:tableview2];
tableview3.frame = CGRectMake(300,0,100,100);
[scrollview addSubview:tableview3];
scrollview.contentSize = CGSizeMake(400,100);
scrollview.pagingEnabled = YES;

Resources