Add existing view controllers in scrollview - ios

I have two view controllers and I wanted to add this as subview of my scrollview, but, when I add these view controllers the second controller is only displayed.
This is my code.
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
[scrollView setBounces:NO];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460);
controllers = [[NSMutableArray alloc] initWithCapacity:0];
First *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
[scrollView addSubview:first.view];
[controllers addObject:first];
Second *second = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
[scrollView addSubview:second.view];
[controllers addObject:second];
}
Thanks in advance.

Just give you an example, you can adjust their frame according to your requirement.
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
[scrollView setBounces:NO];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460);
controllers = [[NSMutableArray alloc] initWithCapacity:0];
First *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
[scrollView addSubview:first.view];
first.view.frame = CGRectMake(0.0, 0.0, 320.0, 460.0) ;
[controllers addObject:first];
Second *second = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
[scrollView addSubview:second.view];
second.view.frame = CGRectMake(320.0, 0.0, 320.0, 460.0) ;
[controllers addObject:second];
}

Related

Extra White Space in Horizontal Scrollview of UITableViews

Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[self colorWithHexString:#"d944f7"]];
self.pageControl = [[UIPageControl alloc] init];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, self.view.frame.size.height)];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0);
[self.view addSubview:self.scrollView];
}
- (void)reloadData
{
self.pageControl.numberOfPages = self.sectionsArray.count;
self.pageControl.currentPage = 0;
self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
self.pageControl.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height - 20);
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.pageControl];
self.scrollView.contentOffset = CGPointMake(0, 0);
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*self.pageControl.numberOfPages, self.scrollView.frame.size.height);
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *mvcs = [NSMutableArray array];
NSMutableArray *shadows = [NSMutableArray array];
for (int i = 0 ; i < self.pageControl.numberOfPages; i ++) {
UIView *shadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height - 80)];
shadow.backgroundColor = [UIColor grayColor];
shadow.alpha = 1;
CGPoint pageCenter = CGPointMake((i + 0.5)*self.scrollView.frame.size.width, self.scrollView.frame.size.height/2.0);
shadow.center = pageCenter;
// [shadows addObject:shadow];
//[self.scrollView addSubview:shadow];
MDIMovingView *mv = [[MDIMovingView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 5, self.scrollView.frame.size.height - 80)];
mv.identity = 1;
mv.delegate = self;
mv.moveEnabled=NO;
mv.backgroundColor = [self colorWithHexString:#"e6e6e6"];
mv.center = pageCenter;
mv.layer.cornerRadius=10;
mv.clipsToBounds=NO;
[array addObject:mv];
[self.scrollView addSubview:mv];
UIView *topHeader=[[UIView alloc] initWithFrame:CGRectMake(0, 0, mv.frame.size.width, 50)];
[topHeader setRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10.0];
[topHeader setBackgroundColor:[self colorWithHexString:#"dca2e8"]];
[mv addSubview:topHeader];
UIView *invisibleSeperator =[[UIView alloc] initWithFrame:CGRectMake(0, 50, mv.frame.size.width, 2)];
[invisibleSeperator setBackgroundColor:[self colorWithHexString:#"d944f7"]];
[mv addSubview:invisibleSeperator];
MDITableViewController *vc = [[MDITableViewController alloc] init];
[mvcs addObject:vc];
vc.objects = self.sortedTasksArray[i];
vc.delegate = self;
vc.dataSoure = self;
vc.movingViewDelegate = self;
[self addChildViewController:vc];
vc.view.frame = CGRectMake(5, 60, mv.frame.size.width - 10, mv.frame.size.height - 80);
[mv addSubview:vc.view];
}
self.shadowViews = shadows;
self.movingViews = array;
self.tableVCs = mvcs;
}
Here is what its doing- note the space between navigation bar and the movingview:
You have to disable the automaticallyAdjustsScrollViewInsets in your view controller
[self setAutomaticallyAdjustsScrollViewInsets:NO];
or in Storyboard
Uncheck the option

iOS8: Not show UIPopoverController but iOS7 is OK

Apple seems to change the attitude of UIPopoverController since iOS8.
Please look at the code below. I want to show UIPopoverController after addSubview. iOS6 and iOS7 are no problem but it does not display on iOS8.
Could you suggest any possible reasons? Any help will be appreciated it.
- (IBAction)tapButton:(id)sender {
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
[UIScreen mainScreen].bounds.size.height / 2,
200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
[self.view.window addSubview:sampleView];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
}
// On iOS7 is OK but not show on iOS8
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:sampleView.label
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
Update1:
I followed by this question's answer.
in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more
This is updated code.
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
As far as I checked, keyWindos and self.view.window seems to be same.
NSLog(#"%#", [UIApplication sharedApplication].keyWindow);
NSLog(#"%#", self.view.window);
However, this is not complete solution for my case. I want to control views on the added subview.
Update2:
I guess that I need to use UIPopoverPresentationController on iOS8.
- (IBAction)tapButton:(id)sender {
// View
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
// Label
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
// View Controller
UIViewController *viewController = [[UIViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationPopover;
viewController.view = sampleView;
// UIPopoverPresentationController
UIPopoverPresentationController *presentationController = viewController.popoverPresentationController;
[presentationController setDelegate:self];
presentationController.permittedArrowDirections = 0;
presentationController.sourceView = [[UIApplication sharedApplication] keyWindow];
presentationController.sourceRect = [[UIApplication sharedApplication] keyWindow].bounds;
[viewController setPreferredContentSize:CGSizeMake(320, 480)];
[self presentViewController:viewController animated:YES completion:nil];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
vc.modalPresentationStyle = UIModalPresentationPopover;
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
self.popover.delegate = self;
}
[self.popover presentPopoverFromRect:CGRectMake(300, 300, 0, 0)
inView:viewController.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
Update3 I used dispatch_async but not worked. Some say that dispatch_async is effective in this case. dispatch_async is very simple solution. I prefer to use it. If you can fix my problem by using dispatch_async, I would be grateful you could attache the code.
- (IBAction)tapButton:(id)sender {
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
[UIScreen mainScreen].bounds.size.height / 2,
200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
[self.view.window addSubview:sampleView];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
}
// I tried this but not worked.
dispatch_async(dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:sampleView.label
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});
}
Try and do the following for iOS 8
dispatch_async( dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) inView:sampleView.label permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});
EDIT
This is my code, it works With or without dispatch_async.
self.popover = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:self.button.frame inView:self.button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});

UIPopoverController is not presenting controller in assigned position

I am trying to present a popover on clicking a button. When I tap on button, it shows popover on top of the screen and background is completely black. I have set the position and gave hard coded origin to popover but still it is going on top.
Here is the snapshot:
Here is the code I am using:
UIPopoverController *popOverController = [[UIPopoverController alloc]
initWithContentViewController:myViewController];
CGRect frame = CGRectMake(20, 30, 100, 50);
popOverController.popoverLayoutMargins = UIEdgeInsetsMake(0, frame.origin.x, 0, 0);
popOverController.delegate = self;
[popOverController setBackgroundColor:[UIColor clearColor]];
[popOverController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
EDIT
I have changed my code and tried this:
UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, 50)];
callOut.backgroundColor = [UIColor whiteColor];
UILabel *callOutLable = [[UILabel alloc] initWithFrame:CGRectMake(callOut.frame.origin.x-5, callOut.frame.origin.y-5, 80, 40)];
[callOutLable setText:#"Callout"];
[callOut addSubview:callOutLable];
UIViewController *controller = [[UIViewController alloc] init];
controller.view = callOut;
UIPopoverController *popOverController = [[UIPopoverController alloc]
initWithContentViewController:controller];
popOverController.delegate = self;
CGRect frame = callOut.frame;
frame.size.height = 100;
frame.size.width = 100;
popOverController.popoverContentSize = CGSizeMake(100.0, 50.0);
[popOverController setBackgroundColor:[UIColor blueColor]];
[popOverController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
As I am creating a custom view which is really small. Now it shows the label on assigned position but still the background view is white.
Now what I am missing here? Any clue?
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
testViewController.delegate = self;
self.userDataPopover = [[UIPopoverController alloc] initWithContentViewController:testViewController];
self.userDataPopover.popoverContentSize = CGSizeMake(320.0, 400.0);
[self.userDataPopover presentPopoverFromRect:[(UIButton *)sender frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
See this example, you should give content size to pop over as above. But you declared directly as CGRect.

Arrange UIscrollView to back

Maybe a huge noob question but i cant seem to figure it out, even after some time googling i cant find out to arrange a scrollview underneeth a navigation bar.
My code:
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;
}
Try inserting the view behind the superview or try sendSubviewToBack.
[[self.view superview] insertSubview:scroll belowSubview:self.view];
or
[self.view sendSubviewToBack:scroll];
It sounds like you are just wanting to add the scroll view to the self.view though. Do as the other answer suggest and create the scrollView from the self.view frame then add it to your self.view.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame]; // Thilina
[self.view addSubview:scroll];
Since you are passing the frame of [UIScreen mainScreen] at the time of allocating the UIScrollView , the size of UIScrollView is overlapping the UINavigation . Replace it with
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
also rather than replacing the self.view with the scrollView , try to add the scrollView as subview on self.view
[self.view addSubview:scrollView];
Do the following task in the viewDidLoad
Hope it will help you.
You can simply use the self.view's frame when creating the ScrollView. This will solve your problem.
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
[self.view addSubView:scroll]; //MarkM
}

Trying to construct a tableview with a navigation bar at top

Here's the code I used. What am I missing?
- (void)loadView
{
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
}
Instead of a nav bar with a table underneath, I get a black screen under the status bar.
You need to create a containing view in your loadView method and set it as the view on your view controller:
- (void)loadView {
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screen_size.width,screen_size.height)];
self.view = myView;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
[myView release];
}
Or alternately, if you have a nib file associated with your view controller then you should be using the viewDidLoad method instead of loadView.

Resources