I am trying to make a UIScrollView scrollable.As I've understood, the difference between the content size and the frame size makes the view scrollable to reach the other parts of the rectangle.So I've put different sizes.
This what I've done in my view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView= [[UIScrollView alloc]initWithFrame: CGRectMake(0, 0, 200, 200)];
scrollView.contentSize= CGSizeMake(1000, 1000);
scrollView.scrollEnabled=YES;
scrollView.pagingEnabled=YES;
scrollView.backgroundColor=[UIColor yellowColor];
scrollView.alwaysBounceHorizontal=YES;
scrollView.alwaysBounceVertical=YES;
[self.view addSubview: scrollView];
}
But the view is not scrollable, the scroll bar isn't even there is the view.
There are some similar questions but no one made me get the solution.
I got the impression that everything I write in the viewDidLoad method is merely ignored, except for setting the color.But of course the method gets executed.
make sure to include (do this in viewWillAppear, because there are some strange issues with declaring this in viewDidLoad)
self.scrollView.delegate=self;
and also declare that your class is a UIScrollViewDelegate in your header somewhere
It looks to me as if you are trying to create a scrollview without any subviews? I guess it doesn't really want to have an arbitrary contentSize if it doesn't really have anything to show anyways. What is it exactly that you are trying to achieve?
UIScrollView class reference
I discovered that the view was scrolling, just in a different way from what I had habis with Cocoa so i didn't notice it.
Related
I dont have a very good understanding on the topic, so please bear with me
I am trying to display a UIViewController inside another UIViewController. I feel like there aren't enough examples out there. I have read the documentation but being fairly new to iOS I am kind of lost.
Here is my problem: I can get the child UIViewController to appear inside the parent UIViewController but the child view is not displaying right.
Sorry, I cant post pictures yet.
Here is my child - UIViewController.
Here is my parent - UIViewController. (The blue area is where the child view is added)
Here are the results:
Blue UIView in place - http://i.imgur.com/4vny8EZ.png
If i move the blue UIView over to the right here is what happens
Blue UiView moved over to the right - http://i.imgur.com/XCOBwr6.png
I have tried a lot of different things but my lack of knowledge on the subject is really making it difficult to find out what is wrong.
Here is how I am loading my child UiViewController in my parent UIViewControllers class:
- (void)viewDidLoad
{
[super viewDidLoad];
// Add Calculations View Controller
CalculationViewController *calculationController = [self.storyboard instantiateViewControllerWithIdentifier:#"Calculate"];
[self addChildViewController:calculationController];
calculationController.view.frame = self.calcualtionView.bounds;
[self.calcualtionView addSubview:calculationController.view];
}
So, my question is why is it displaying the child view in a weird fashion?
Thank you!
Also, sorry about the links I could only post two.
Quick answer: Views sizes aren't determined until viewWillAppear. Set the frame there and it will work.
Longer answer:
1) If you are using iOS6, I would encourage you to use NSLayoutConstraints to deal with view size and positioning rather than manually setting view frames. It takes a bit to really wrap your head around constaints, but once you do they are much more powerful and flexible than setting frames and you won't have to worry about when or how your IBOutlets are created
2) You are not adding your child view controller correctly. You must call didMoveToParentViewController: after the view is added. With the missing line of code as well as using contraints rather than frames your code looks like this:
CalculationViewController *calculationController =
[self.storyboard instantiateViewControllerWithIdentifier:#"Calculate"];
calculationController.translatesAutoresizingMaskIntoConstraints = NO;
[self addChildViewController:calculationController];
[self.calcualtionView addSubview:calculationController.view];
[calculationViewController didMoveToParentViewController:self];
// Add constraints for the view to fill calculationView
UIView *calcView = calculationController.view;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(calcView);
NSArray *hConstraint =
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[calcView]|"
options:0 metrics:nil
views:viewsDictionary];
[self.calculationView addConstraints:hConstraint];
NSArray *vConstraint =
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[calcView]|"
options:0 metrics:nil
views:viewsDictionary];
[self.calculationView addConstraints:vConstraint];
Move the code to viewDidAppear. If calculationView is an IBOutlet to a view, its bounds will be zero in viewDidLoad. The frames of views are not set until viewDidAppear.
I am trying to just do a simple view change for proof of concept.
here is the code
- (void)swipedScreen
{
if (self.secondView.superview == nil) {
[myView removeFromSuperview];
[self.view insertSubview:secondView atIndex:0];
}
}
when I swipe the screen what happens is the view area just goes black... and becomes unresponsive.
I started with a navigatoin app, replaced the tableview with just a standard uiviewcontroller class.. that worked fine..Then i added a secondView (xib only) and changed its class to match the viewcontroller of the first view.
The reason I am finding this difficult is because i am trying to animate the views inside the navigation controller and not push a whole view onto the stack which I am used to doing.
I'll bet that blank unresponsive view is, in reality, your secondView object. I always test by setting [secondView setBackgroundColor:[UIColor greenColor]] and checking if the massive green rectangle actually shows up.
EDIT: having looked at your code, there are multiple problems that arose:
You never actually +alloc or -init anything.
You never actually touch those nibs or make a reference to them in code
You declare two UIView's as IBOutlets and Strong (two exact opposites, as IBOutlets are __weak, __unsafe_unretained, or assign), yet do not link them to anything.
I've taken the liberty of revising it (sans nibs). Take a look.
Did you init the secondView? if init,you can try to set frame for the secondView
Your inserting the view at the bottom of the stack,
[self.view insertSubview:secondView atIndex:0];
Try using addSubview instead. Also you need to set the views frame somewhere.
Can't seem to find a way to fix a graphic - a light graphic that would remain static as the user navigates from scene to scene. Have been combing forums for a few days, but most answers point to MainWindow.xib, which isn't generated for a storyboard project. Would appreciate any advice.
Here’s what I was able to cobble together thanks to advice from #Max. Like I said, very new to this, so if this code needs tuning please leave a comment so I don’t lead others astray.
Add the image to the app’s main window in the AppDelegate’s didFinishLaunchingWithOptions method :
UIImageView *myGraphic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myGraphic.png"]];
[self.window.rootViewController.view addSubview: myGraphic];
[self.window.rootViewController.view sendSubviewToBack: myGraphic];
Then, to make your views transparent so the background shows through - in your viewController class/es, add to viewDidLoad method:
self.view.backgroundColor = [UIColor clearColor];
(And note if you try to decrease the transparency through the alpha property for the view via the Attributes Inspector, it will make everything transparent on that view -buttons, etc - that’s why it needs to be done programmatically.)
Sure, it’s not thoroughly tested. But for now it’s working and it’s a thing of beauty. Thanks all.
You can try to manually add UIImageView to your window and then set 0 background transparency
for all other views.
One way to do it -- it may not be the best -- can be that you subclass a UIView, and set the UIView instance in all of your viewControllers in the storyboard an instance of your cutomized UIView class, which contains your graphic as a background.
If you're going to tackle this programmatically, within each view controller, set:
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
in viewDidLoad
I'm sure some tweaking would allow you to apply this principle throughout the program from the app delegate, or by using a singleton.
Using this solution, though, you're still going to have to make some method call within each view controller where you want this style applied.
I wonder, how and when (viewDidLoad, viewWillAppear, viewDidAppear) can I get a UIViews frame size that was autoresized to fit its partent view?
It's not clear from your question why you want it but I imagine it is to layout your sub views. Luckily Apple knew you would want to do that and implemented -(void)layoutSubViews see this: When is layoutSubviews called?
I tried layoutSubviews, but it gets called more than once which is a problem when I need the auto sized frame in order to create a CGContext to draw into.
The solution I used was to expose an init method on my custom UIView for its parent view controller to call in its viewWillAppear.
I wish there was a way to accomplish this all within the custom UIView's implementation, but it eludes me (and Google and Apple's docs and SO).
- (void)viewDidAppear:(BOOL)animated {
CGFloat uIViewWidth = self.uIView.bounds.size.width;
CGFloat uIViewHeight = self.uIView.bounds.size.height;
CGRect uIViewSize = CGRectMake(0, 0, uIViewWidth, uIViewHeight);
}
NSLog(#"%f; %f; %f; %f;", yourView.frame.origin.x, yourView.frame.origin.y, yourView.frame.size.width, yourView.frame.size.height);
this is how you can get frame of your view
I've got my own dynamically changing view inside scrollview.
As my view is very big I usually redraw only it's visible part.
But when I scroll up or down drawRect method isn't being called.
I guess scroll view has a buffer to quickly react on user actions but I don't know exactly a mechanism how it works.
UPD
Thanks to Wienke, I've got a solution: to implement UIScrollViewDelegate.
I've implemented scrollViewDidScroll method:
-(void) scrollViewDidScroll:(UIScrollView *)sender {
CGRect visibleRect;
visibleRect.origin = [scrollView contentOffset];
visibleRect.size = [scrollView bounds].size;
[textField setNeedsDisplayInRect:visibleRect];
}
So every time user scrolls even a little this method redraws the whole visible part. That's bad=(
How can I redraw only... Let me call that "new region that appeared after scrolling". I guess it's much faster...
Thanks for your attention.
Have you tried assigning a scrollview delegate? The UIScrollViewDelegate protocol includes methods like scrollViewDidEndDragging, upon which you could redraw.