UITableView Scrolling Horizontal issue - ios

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

Related

UITableView over scroll to bottom but top is normal

added in viewDidLoad
navgationBar.translucent =N0, iOS 10.3, xcode 8.3
- (void)__setUpUI{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
[tableView registerNib:[UINib nibWithNibName:#"DXGroupMasterLikesCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([DXGroupMasterLikesCell class])];
_tableView = tableView;
_tableView.backgroundColor = [UIColor orangeColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView];}
like this
but if I set [self.view addSubview:[UIView new]]; before [self.view addSubview:tableView]; everything is ok
somebody know why ?

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.

TableView starts in the wrong position initially

This issue has come about as I update an app from iOS 7 to iOS 8. My tableview starts in the wrong position initially. Once you touch it or scroll it. The tableview scrolls into the correct position and works as intended.
The table view is embedded within a UIPageViewController.
UITableView *tableView = (id) [self.view viewWithTag:5];
[tableView registerClass:[BroadcastCell class] forCellReuseIdentifier:CellBroadcastTableIdentifier];
[tableView registerClass:[BroadcastHeaderCell class] forCellReuseIdentifier:CellBroadcastHeaderTableIdentifier];
[tableView setSeparatorInset:UIEdgeInsetsZero];
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 64.0f)];
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 0.0f)];
[tableView setDelegate:self];
[tableView setDataSource:self];
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = UITableViewAutomaticDimension;
Screenshot can be found - https://pbs.twimg.com/media/Byh_5eIIQAA06ok.png:large
I had the same issue. After much effort I found out that in the previous view I was using
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
I changed it to
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
and the problem was solved.
Select your View Controller and Deselect property 'Adjust Scroll View Insets' on the storyboard (under View Controller section).
OR
Add following lines
self.automaticallyAdjustsScrollViewInsets = NO;

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

Adding UISearchBar to UITableView

Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.
Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.
Code:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Date/Time", #"Location", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.
Why does it resize at all and how can I get it to stop?
EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?
i've been banging my head on this for too freakin long... props to this dude here for the clues.
if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.mySearchBar removeFromSuperview];
self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
[self.table.tableHeaderView addSubview:self.mySearchBar];
}
As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"
...yay apple.
Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.
Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
[_searchBar setShowsCancelButton:YES];
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Title", #"Date/Time", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];
self.tableView = _tableView;
}
This meant that I had:
UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.
If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.

Resources