Cannot prevent UITableView from moving when bounds change - ios

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);
}

Related

Custom View AddSubview programmatically, not shown UIElements…

I do this to add a Subview from Storyboard, which is a subview from a ViewController with its own Class. (TomStatusbarController) ...it is set as CustomClass in Storyboard. I do also Constraints with Layoutformats. This works fine, but the Statusbar is just red and i see no UIElements. I get lots on constraints warnings but it worked before, when i had it as an IBOutlet, but I have to change this now.
-(void)viewDidLoad {
[super viewDidLoad];
if (self) {
self.tomCaptureStatus = [[TomStatusbarController alloc] initWithFrame:CGRectMake(0, 0, 375, 78)];
self.tomCaptureStatus.layer.bounds = CGRectMake(0, 0, 375, 78);
self.tomCaptureStatus.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
self.view.translatesAutoresizingMaskIntoConstraints = YES;
self.tomCaptureStatus.backgroundColor = [UIColor redColor];
[self.view addSubview:self.tomCaptureStatus];
[self.view superview];
}
}
So why i don't see any thing, expect my backgroundcolor?
Tom

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.

Increase height of UIView without Auto Layout

I need to create an "old" style UIView, without AutoLayout and constraints. In my view, there is a table view which is calculating it's size with auto layout and another view. Unfortunately it appears, that the view itself doesn't provide the height I need. This is how I am creating the UIView:
// View 1 - Calendar View
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, height)];
calendar.dataSource = self;
calendar.delegate = self;
calendar.scopeGesture.enabled = YES;
[view addSubview:calendar];
// View 2 - TableView
_myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
self.myTableView.tableFooterView = [UIView new];
self.myTableView.backgroundColor = self.view.backgroundColor;
[self.myTableView setShowsHorizontalScrollIndicator:NO];
[self.myTableView setShowsVerticalScrollIndicator:NO];
// self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.myTableView.estimatedRowHeight = 85.0;
self.myTableView.rowHeight = UITableViewAutomaticDimension;
_myTableView.bounces = NO;
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
self.myTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
if (SYSTEM_VERSION_GREATER_THAN(#"8.4") && (IS_IPAD)){
self.myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
[self.view addSubview:_myTableView];
I suppose problem might be here:
_myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
Because, I'm missing about 200 points of space (y position that I want to programmatically move the table view down).
Is there any way to fit screen height to my views?
If this view is inside another view, it should be resized in layoutSubviews.
- (void)layoutSubviews
{
[super layoutSubviews];
// your code here...
}
If it is inside of a viewController's view do the resizing in viewDidLayoutSubviews.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// your code here...
}

UIImageView under UITableView does not show

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.

UITableView Scrolling Horizontal issue

this is my code for creating tableView :
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(8, 0, self.view.frame.size.width - 16, self.view.frame.size.height - 8) style:UITableViewStylePlain];
[_tableView registerNib:[UINib nibWithNibName:#"commentCell" bundle:nil] forCellReuseIdentifier:#"commentCell"];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
the Problem is that my table is scrolling horizontally I need to know how to deactivate it, I've tried bounce = NO; or contentSize but always same issue my table still allow scrolling horizontally
The Issue was caused by [tableView reaload] function in my viewDidAppear , which reload my table before finishing displaying allCell so this caused inappropriate behaviour

Resources