Increase height of UIView without Auto Layout - ios

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...
}

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.

How can I finished one view's constrains before other view generate

Now, I am going to generate a titleview that its height is variable for the tableHeaderView.so i need to confirm the titleview's hegiht before the tableview generate
//titleView
JMProductTitleView *titleView = [[JMProductTitleView alloc]initWithFrame:CGRectMake(0, 0, JMDeviceWidth, 300)];
titleView.delegate = self;
JMProductDetailModel *model = [JMPorductDetailTool createProductDetailModel];
titleView.model = model;
_titleView = titleView;
//
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, JMDeviceWidth, JMDeviceHeight) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableHeaderView = titleView;
[self.view addSubview:tableView];
_tableView = tableView;
You can try using [self.view layoutIfNeeded]. It will force the view to layout all the constraints.

UIScrollView doesn't scroll in the bottom part of screen

I'm using a UIScrollView with 1 image and 2 tableview inside. The scroll works correctly if I tap on the center and on the top of the screen but doesn't works if I try to scroll tapping on the bottom of the screen. Here the code that I used.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
self.scrollView.scrollEnabled = YES;
self.scrollView.delegate = self;
[self addSubview:self.scrollView];
self.theImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, lenghtImage, hightImage)];
self.theImage.image = [UIImage imageNamed:#"theImage.png"];
[self.scrollView addSubview:self.theImage];
self.tableA = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosA, self.bounds.size.width, heightA];
self.tableA.delegate = self;
self.tableA.dataSource = self;
self.tableA.scrollEnabled = NO;
[self.scrollView addSubview:self.tableA];
self.tableB = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosB, self.bounds.size.width, hightB)];
self.tableB.delegate = self;
self.tableB.dataSource = self;
self.tableB.scrollEnabled = NO;
[self.scrollView addSubview:self.tableB];
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,heightImage+heightTableA+heightTableB);
Your scroll view contentSize should be equal or greater than its total subview size.
You calculate contentSize height by adding all of its subview height and also their y coordinate.
so it should be:
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, hightImage + yPosA + heightA + yPosB + hightB);

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.

Resources