scrollView incorrect position xcode 5 - ios

I don't know why I always get stuck with scrollview's position issue... I just want to display scrollview after a label at 5 px distance, but its taking extra margin on top. I am using storyboard - xcode 5. Scrollview positions correctly when I test with iOS 7, but creates problem in iOS 6 simulator.
Note: I am not using any constraints.
In storyboard,
iOS 6 snapshot
iOS 7 snapshot
Simple piece of code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
if([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)])
self.automaticallyAdjustsScrollViewInsets = NO;
//add subviews to scrollview
_latestNewsLabel.layer.borderColor = [UIColor greenColor].CGColor;
_latestNewsLabel.layer.borderWidth = 1.0;
_latestNewsScroll.layer.borderColor = [UIColor blueColor].CGColor;
_latestNewsScroll.layer.borderWidth = 1.0;
self.view.backgroundColor = [UIColor whiteColor];
_categoriesList = [NSArray arrayWithObjects:#"US World", #"World", #"Business", #"Political", #"Entertainment", #"Sports", #"IT", nil];
selectedIndex = -1;
[_collectionView registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:#"Category"];
CollectionViewLayout *collLayout = [[CollectionViewLayout alloc] init];
[self.collectionView setCollectionViewLayout:collLayout];
}
-(void) viewWillAppear:(BOOL)animated{
CGRect newFrame = _latestNewsScroll.frame;
newFrame.origin.y = _latestNewsLabel.frame.origin.y + _latestNewsLabel.frame.size.height + 5;
_latestNewsScroll.frame = newFrame;
newFrame = _collectionView.frame;
newFrame.origin.y = _latestNewsScroll.frame.origin.y + _latestNewsScroll.frame.size.height + 10;
_collectionView.frame = newFrame;
}

Related

after change my tableframe its start scroll horizontally?

I create a table view in story board. when i change my tableView frame it become horizontal scroll and stop vertical scrolling. i am using auto resizing.
Code on viewDidLoad for table:
_inviTableView.layer.cornerRadius = 5;
_inviTableView.backgroundColor = [UIColor whiteColor];
_inviTableView.layer.masksToBounds = NO;
_inviTableView.layer.shadowOffset = CGSizeMake(0, 0);
_inviTableView.layer.shadowRadius = 10.0;
_inviTableView.layer.shadowOpacity = 0.5;
_inviTableView.clipsToBounds = YES;
[_inviTableView.layer setShadowColor:[UIColor blackColor].CGColor];
_inviTableView.delegate = self;
_inviTableView.dataSource = self;
[self.inviTableView registerClass:[inviYourBoastTableViewCell class] forCellReuseIdentifier:#"inviCell1"];
[self.inviTableView registerNib:[UINib nibWithNibName:#"inviYourBoastTableViewCell" bundle:Nil] forCellReuseIdentifier:#"inviCell1"];
self.inviTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
ViewDidAppear code:
#pragma mark:didappear
-(void)viewDidAppear:(BOOL)animated{
orignalInviTblFrame = _inviTableView.frame;
}
Method where i changed my table frame:
-(IBAction)getYourTopBoastValue:(UIButton*)senders{
inviDetailViewDataIndex = -1;
inviTopDetailViewIndex = (int)senders.tag-1;
[_inviDetailTableView reloadData];
self.inviTableView.frame = CGRectMake(orignalInviTblFrame.origin.x,_inviSelectedView.frame.size.height, orignalInviTblFrame.size.width, orignalInviTblFrame.size.height-_inviSelectedView.frame.size.height);
_inviSelectedView.hidden = NO;
[_inviTableView reloadData];
}
Note:_inviSelectedView is another tableView

Scroll view not scrolling when more than one subview added [duplicate]

This question already has answers here:
UIScrollView not scrolling
(27 answers)
Closed 7 years ago.
I'm adding subViews to a programmatically added UIScrollView, yet when the subViews go off the screen the scrollview isn't scrolling. Any Ideas?
I need to update the content size of the scrollview more than once.
First I add the scrollView itself as subView of the view, like so:
[super viewDidLoad];
[self addScrollView];
[self APISimpleDemo];
}
#pragma mark Add Scroll View
-(void)addScrollView {
//Add Scroll View
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
CGRect scrollFrame;
scrollFrame.size.height = self.view.frame.size.height - (navHeight - 150); //set height minus nav bar and top section
scrollFrame.size.width = self.view.frame.size.width;
scrollFrame.origin.y = 180;
scrollFrame.origin.x = 0;
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
NSLog(#"TableView Frame %#", NSStringFromCGRect(scrollFrame));
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
}
The NSLog output is: {{0, 180}, {375, 773}}
Next the code runs through and the end of each test (in this example 2) a tableView is added to the newly added scrollView. Like so...
-(void)addSpeedTable {
CGRect frame = self.tableView.frame;
frame.size.height = (40 * resultHeadings.count)+35;
frame.size.width = self.view.frame.size.width;
//If more than one table has been shown increase Y axis height for view
if(tablesDisplayed > 0) {
NSLog(#"Tables displayed greater than zero");
viewHeight = viewHeight + frame.size.height;
}
frame.origin.y = viewHeight;
frame.origin.x = 0;
self.tableView = [[UITableView alloc] initWithFrame:frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 40;
self.tableView.sectionFooterHeight = 0;
self.tableView.sectionHeaderHeight = 22;
self.tableView.scrollEnabled = NO; // Disable ScrollView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(#"TableView Frame %#", NSStringFromCGRect(frame));
[self.scrollView addSubview:self.tableView];
tablesDisplayed++;
}
As I needed the content size of the scrollView to increase as TableViews were added to the ScrollView I created a method that updates the scrollView content size every time a table is added:
The code now looks like:
-(void)addScrollView {
//Add Scroll View
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
CGRect scrollFrame;
scrollFrame.size.height = self.view.frame.size.height - navHeight - 150; //set height minus nav bar and top section
scrollFrame.size.width = self.view.frame.size.width;
scrollFrame.origin.y = 180;
scrollFrame.origin.x = 0;
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
}
#pragma mark Update Scroll View Content Size
-(void)updateScrollSize {
//View height is set in the add table method depending on the size of the table
CGFloat scrollHeight = 400 + viewHeight;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, scrollHeight);
}
-(void)addSpeedTable {
CGRect frame = self.tableView.frame;
frame.size.height = (40 * resultHeadings.count)+50;
frame.size.width = self.view.frame.size.width;
//If more than one table has been shown increase Y axis height for view
if(tablesDisplayed > 0) {
NSLog(#"Tables displayed greater than zero");
viewHeight = viewHeight + frame.size.height;
}
frame.origin.y = viewHeight;
frame.origin.x = 0;
[self updateScrollSize]; //Update scrollView contentSize when adding new table to view
self.tableView = [[UITableView alloc] initWithFrame:frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 40;
self.tableView.sectionFooterHeight = 0;
self.tableView.sectionHeaderHeight = 22;
self.tableView.scrollEnabled = NO; // Disable ScrollView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(#"TableView Frame %#", NSStringFromCGRect(frame));
[self.scrollView addSubview:self.tableView];
tablesDisplayed++;
}
You are not setting scrollView.contentSize.

App crashes when UIScrollView touched

I have a RootViewController which creates a SecondViewController. Inside the SecondViewController in viewDidAppear, I create a UIScrollView and a UIPageControl and add them to the view. I also do this: scrollview.delegate = self. The SecondViewController is a <UIScrollViewDelegate>. I have also implemented scrollViewDidScroll inside the SecondViewController. All of this works, compiles and runs. However, when I touch the UIScrollView, the app crashes. For the life of me, I cannot figure out why. It is the stupidest problem, but I cannot solve it.
It is very similar to this problem: UIScrollView EXC_BAD_ACCESS crash in iOS
However, I tried those solutions and none of them work.
I have pasted some code below. I really appreciate the help.
RootViewController:
- (void)viewDidAppear:(BOOL)animated
{
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.view.frame = CGRectMake(50, 50, 925, 600);
secondViewController.view.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:secondViewController.view];
}
SecondViewController:
- (void)viewDidAppear:(BOOL)animated
{
CGRect scrollViewFrame = CGRectMake(50, 50, 824, 500);
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*3,
self.scrollView.frame.size.height);
self.scrollView.backgroundColor = [UIColor darkGrayColor];
self.scrollView.showsHorizontalScrollIndicator = YES;
self.scrollView.showsVerticalScrollIndicator = YES;
self.scrollView.pagingEnabled = YES;
[self.view addSubview:self.scrollView];
CGRect pageControlFrame = CGRectMake(0, 0, 50, 20);
self.pageControl = [[UIPageControl alloc] initWithFrame:pageControlFrame];
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
self.pageControl.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.pageControl];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollView.frame.size.width;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pageControl.currentPage = page;
}
your secondViewController object is deallocated as it is not retained anywhere only its view is retained as it is added as subview. You also need to retain secondViewController's object.

How to prevent UIView from being resized to fit ScrollView height (autoresizing disabled)?

I'm writing a PDF reader using vfr-reader library. To display two pages in landscape i'm rendering each page into its own view, then add these two views to a container view, then add container view to a scroll view. Each view's autoresizingMask is set to UIViewAutoresizingNone, contentMode is UIViewContentModeRedraw, autoresizingSubviews is set to 'NO' for all views.
But still somehow container view is autoresized to fit scroll view's height, and I don't know where is this happening. I care about this because, when autoresizing container view, it's width becomes larger than screen width, and I cannot scroll to the next two pages with a single swipe (two swipes are needed), which sucks. What am I missing?
EDIT gonna add some come if it helps. In the ViewController I create a Scroll View with following options:
theScrollView = [[ReaderScrollView alloc] initWithFrame:viewRect];
theScrollView.scrollsToTop = NO;
theScrollView.pagingEnabled = YES;
theScrollView.delaysContentTouches = NO;
theScrollView.showsVerticalScrollIndicator = NO;
theScrollView.showsHorizontalScrollIndicator = NO;
theScrollView.contentMode = UIViewContentModeRedraw;
theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.backgroundColor = [UIColor clearColor];
theScrollView.userInteractionEnabled = YES;
theScrollView.autoresizesSubviews = NO;
theScrollView.delegate = self;
[self.view addSubview:theScrollView];
When I'm drawing pages I'm adding an UIView to the Scroll View, which is being initiated this way:
if ((self = [super initWithFrame:frame]))
{
self.autoresizesSubviews = YES;
self.userInteractionEnabled = YES;
self.contentMode = UIViewContentModeRedraw;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor clearColor];
theScrollView = [[ReaderScrollView alloc] initWithFrame:self.bounds]; // Not sure about this part - why is the 2nd scroll view added?
// this is the way its done in vfr reader
theScrollView.scrollsToTop = NO;
theScrollView.delaysContentTouches = NO;
theScrollView.showsVerticalScrollIndicator = NO;
theScrollView.showsHorizontalScrollIndicator = NO;
theScrollView.contentMode = UIViewContentModeRedraw;
theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.backgroundColor = [UIColor clearColor];
theScrollView.userInteractionEnabled = YES;
theScrollView.autoresizesSubviews = NO;
theScrollView.bouncesZoom = YES;
theScrollView.delegate = self;
theContentView = [[ReaderContentPage alloc] initWithURL:fileURL page:page password:phrase landscape:(BOOL)isLandscape position:FALSE];
CGRect viewRect = CGRectZero;
viewRect.size.width = theContentView.bounds.size.width;
viewRect.size.height = theContentView.bounds.size.height;
if( isLandscape){
NSLog(#"Landscape detected in content view");
if (theContentView == NULL)
{
theContentView = [[ReaderContentPage alloc] initWithURL:fileURL page:(page+1) password:phrase landscape:(BOOL)isLandscape position:FALSE];
theContentView2 = NULL;
viewRect.size.width = theContentView.bounds.size.width;
viewRect.size.height = theContentView.bounds.size.height;
} else {
if (page == 1)
theContentView2 = NULL;
else
theContentView2 = [[ReaderContentPage alloc] initWithURL:fileURL page:(page+1) password:phrase landscape:(BOOL)isLandscape position:TRUE];
if (theContentView2 != NULL)
viewRect.size.width = theContentView.bounds.size.width*2;
}
}
if (theContentView != nil) // Must have a valid and initialized content view
{
theContainerView = [[UIView alloc] initWithFrame:viewRect];
theContainerView.autoresizesSubviews = NO;
theContainerView.userInteractionEnabled = NO;
theContainerView.contentMode = UIViewContentModeRedraw;
theContainerView.autoresizingMask = UIViewAutoresizingNone;
theContainerView.backgroundColor = [UIColor whiteColor];
theScrollView.contentSize = theContentView.bounds.size; // Content size same as view size
theScrollView.contentOffset = CGPointMake((0.0f - CONTENT_INSET), (0.0f - CONTENT_INSET));
theScrollView.contentInset = UIEdgeInsetsMake(CONTENT_INSET, CONTENT_INSET, CONTENT_INSET, CONTENT_INSET);
theThumbView = [[ReaderContentThumb alloc] initWithFrame:theContentView.bounds]; // Page thumb view
[theContainerView addSubview:theThumbView]; // Add the thumb view to the container view
[theContainerView addSubview:theContentView]; // Add the content view to the container view
if(( isLandscape) && (theContentView2 != NULL)){
[theContainerView addSubview:theContentView2]; // Add the content view to the container view
}
[theScrollView addSubview:theContainerView]; // Add the container view to the scroll view
[self updateMinimumMaximumZoom]; // Update the minimum and maximum zoom scales
theScrollView.zoomScale = theScrollView.minimumZoomScale; // Zoom to fit
}
[self addSubview:theScrollView]; // Add the scroll view to the parent container view
[theScrollView addObserver:self forKeyPath:#"frame" options:NSKeyValueObservingOptionNew context:NULL];
self.tag = page; // Tag the view with the page number
}
return self;
And ReaderContentPage is created this way:
if ((self = [super initWithFrame:frame]))
{
self.autoresizesSubviews = NO;
self.userInteractionEnabled = NO;
self.clearsContextBeforeDrawing = NO;
self.contentMode = UIViewContentModeRedraw;
self.autoresizingMask = UIViewAutoresizingNone;
self.backgroundColor = [UIColor clearColor];
view = self; // Return self
}
In the function updateMinimumMaximumZoom in the class ReaderContentView:
The calculation of the zoomscale to fit the screen is done with single view, but in landscape it should be calculated form theContainerView.
Try replacing this code
CGFloat zoomScale = ZoomScaleThatFits(targetRect.size, theContentView.bounds.size);
with
CGFloat zoomScale = ZoomScaleThatFits(targetRect.size, theContainerView.bounds.size);
There is a contentMode property in UIScrollView, change it to UIViewContentModeCenter or something else to prevent resizing.

How to properly implement multiple subview operations in IPAD

I am creating an iPAD app. I have got a MAIN VIEW controller which covers the whole screen. And I have another class DROP-DOWN Class which i will be using to mimic the functionality of a drop-down as in Windows OS components. For that I am using a simple button, a scroll view and a few labels. I instantiate an object of that class and use it in my MAIN VIEW controller. To create the effect of a drop down, I alter the instance's views frame on the touch up of the button in toggle mode.(Click once, frame increased, click again frame decreased).
The creation part was fine, and the function to toggle to worked fine when implemented on a single instance of the DROP DOWN object.
But the problem comes when I instantiate these objects thru a for loop. The views seem to just vanish on button click.
Here is the code within the dropdowns.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10,54,100,50)];
scrollView.contentSize = CGSizeMake(50, 100);
for (int i = 0; i< 10; i++)
{
UILabel *newLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 10*i, 100, 10)];
[newLable setFont:[UIFont boldSystemFontOfSize:10]];
newLable.text = [NSString stringWithFormat:#"Testing %i",i];
[scrollView addSubview:newLable];
}
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
}
-(IBAction)btnClicked:(id)sender
{
if (!isOpen) {
oldRect = self.view.frame;
CGRect newFrame = self.view.frame;
newFrame.size = CGSizeMake(190, 200);
NSLog(#"New Frame - %f,%f,%f,%f",self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.height,self.view.frame.size.width);
self.view.frame = newFrame;
}
else {
self.view.frame = oldRect;
NSLog(#"Old Frame - %f,%f,%f,%f",self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.height,self.view.frame.size.width);
}
isOpen = !isOpen;
}
And here is how this is called from the main view.
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(drawTheFilters)];
}
-(void)drawTheFilters
{
for (int i = 0; i<5 ; i++) {
DropDownView *dropView = [[DropDownView alloc]initWithNibName:#"DropDownView" bundle:[NSBundle mainBundle]];
CGRect newFrame = dropView.view.frame;
newFrame.origin = CGPointMake(200*i, 200);
dropView.view.frame = newFrame;
[self.view addSubview:dropView.view];
NSLog(#"Frame %i, %f,%f,%f,%f",i,dropView.view.frame.origin.x,dropView.view.frame.origin.y,dropView.view.frame.size.height,dropView.view.frame.size.width);
}
}

Resources