Is there any simple way to change UIPageViewControllerNavigationOrientationHorizontal scroll animation - ios

I want the UIPageViewControllerNavigationOrientationHorizontal to scroll from the left instead of the right (the opposite way of the default) in a page view controller.
Can anyone help?

I've been in this situation before and couldn't figure it out using the UIPageViewController. I'm not sure what your intent on your project is but this might help.
Use a UIScrollView and place the amount of UIView's you need inside. At the point you could just create a CGPoint and set your UIScrollView's content offset to your CGPoint. This way you would start at the last page(view) and can scroll to the left instead of the right. Something like this below. You can perform this in your viewDidLoad method.
CGPoint nameYourPoint = CGPointMake(640.0,568,0);
self.yourScrollViewName.contentOffset = nameYourPoint;

I was able to figure it out fairly simply:
When adding objects into the array of images, I added them in backwards like so:
for (int i = 1; i <= 81; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%i.gif", i];
[self.mArray insertObject:[UIImage imageNamed:imageName] atIndex:0];
After that, in the root view controllers viewDidLoad method, I simply started the images at the end of the array
ModelController *objModelController = [ModelController new];
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:[objModelController.mArray count]-1 storyboard:self.storyboard];
note:for some reason the length wasnt working, so I had to just add 2 empty objects to the beginning of the array and use
[objModelController.mArray count]-1
Hope this helps everyone out!

Related

How to search for UIView inside many levels of UIViews?

Is there a better way to search for UIViews children inside 3-4+ levels of UIViews without using tag? Is there a much better way to go directly into the child UIView without having to traverse the tree every time? The tag is really not helping.
Thanks!
if you want to access a view directly you may use
[randomChildView.window.subviews objectAtIndex:0]
in order to avoid looping all subviews you can use of enumerateObjectsUsingBlock
- (NSArray*) allSubviews {
__block NSArray* allSubviews = [NSArray arrayWithObject:self];
[self.subviews enumerateObjectsUsingBlock:^( UIView* view, NSUInteger idx, BOOL*stop) {
allSubviews = [allSubviews arrayByAddingObjectsFromArray:[view allSubviews]];
}];
return allSubviews;
}
Hope it helps!

how can i differentiate and refer to various UITextViews that are created programmatically?

A UITextView is created each time i click on ADD button. Y-axis value is altered(say, y+=100) every time i click ADD and so a set of UITextViews are created one below the other. I cant figure out how to differentiate and access a particular UITextView. Thanks for any help!
EDIT:
-(IBAction)access:(id)sender
{
int tg=[sender superview].tag;
UIView *view=(UIView *)[textView viewWithTag:tg-1];
}
tg-1 because im trying to access the previous UITextView and when i do this it returns NULL.
Store them on a NSMutableArray:
NSMutableArray * views = [[NSMutableArray alloc] init]
Your IBAction
-(IBAction)access:(id)sender{
int tg=[sender superview].tag;
UIView *view=(UIView *)[textView viewWithTag:tg-1];
[views addObject: views];
}
Then you can get all the references with a integer index with:
UIView * storedView = [views objectAtIndex: 1];
Use a view tag to differentiate the views and access them.
You don't say how you're creating the new views, but something like this should work:
UIView* new_view = [UITextView initWithFrame(...)];
new_view.tag = generate_tag()
Where the generate_tag() function generates whatever naming scheme makes sense for your application.

Autolayout Constraints on empty UIView do not work as expected

So I have setup a UIView that contains a UIScrollView (and child content view) that has sub views that are series of UILabels and UIViews that grow and shrink depending on the content contained in them, all using AutoLayout from the Storyboard. This works when I have something like Label - Label - Label - View w/o any issues, however if I put an empty UIView in-between two labels and insert sub views on the UIView, I'm not seeing the results I'm expecting. I have the following layout in a storyboard:
...where the teal and blue views are labels that grow to infinite height and the orange view (optionsPanel) is an empty UIVIew that I later inject sub views into. The rest of the stuff on the window is UILabels and UISegment controls. Between each row of views I have a Vertical Space constraint with a constant of 8. This all worked beautifully until I had to put in the empty UIView and programmatically inject sub views. The code I would expect to work would be something like (optionsPanel is the orange colored UIView)...
optionsPanel.translatesAutoresizingMaskIntoConstraints = YES;
NSArray *options = [product objectForKey:#"options"];
lastTop = 10;
for(int i=0;i<options.count; i++) {
NSDictionary *option = [options objectAtIndex:i];
NSArray *values = [option objectForKey:#"values"];
if([self hasNoneValue:values] && values.count == 2) {
NSDictionary *value = [self notNoneValue:values];
M13Checkbox *optionCheck = [[M13Checkbox alloc] initWithTitle:[option objectForKey:#"name"]];
optionCheck.frame = CGRectMake(0, lastTop, 280, 25);
[optionsPanel addSubview:optionCheck];
lastTop += 25;
} else {}
}
...where the orange UIView would magically grow and everything would just get pushed around accordingly, however this is what I'm seeing:
...the orange UIView does not grow at all, and the other two top UIView have gone somewhere off the screen. So my next guess was to turn off the Autoresizing Mask using...
optionsPanel.translatesAutoresizingMaskIntoConstraints = NO;
...but I'm getting a result where everything appears to be working but the orange UIView (optionsPanel) has no height for whatever reason and looks like:
This is getting closer to what I would expect, so I thought I would force the height of the orange UIView using code like...
frame = optionsPanel.frame;
frame.size.height = lastTop;
optionsPanel.frame = frame;
...but this appears to have no affect on anything.
Purely guessing, I found that this code almost works, if I arbitrary set the optionPanel's origin to something much larger than the space that is needed....
optionsPanel.translatesAutoresizingMaskIntoConstraints = YES;
NSArray *options = [product objectForKey:#"options"];
lastTop = 10;
for(int i=0;i<options.count; i++) {
NSDictionary *option = [options objectAtIndex:i];
NSArray *values = [option objectForKey:#"values"];
if([self hasNoneValue:values] && values.count == 2) {
NSDictionary *value = [self notNoneValue:values];
M13Checkbox *optionCheck = [[M13Checkbox alloc] initWithTitle:[option objectForKey:#"name"]];
optionCheck.frame = CGRectMake(0, lastTop, 280, 25);
[optionsPanel addSubview:optionCheck];
lastTop += 25;
} else {}
}
lastTop += 10;
frame = optionsPanel.frame;
frame.size.height = lastTop;
frame.origin.y += 300; //some arbitrarily‎ large number
optionsPanel.frame = frame;
..which gives this result:
...but apparently the AutoLayout has decided that the name label needs to take up the extra space. Its an ugly approach but if I could figure out how much space I need then I could just push everything down, if I had to. What's the secret to having a dynamic UIView between two dynamically sized labels and everything just work???
As #Timothy says, you need to manually add constraints to the subviews of the orange view if you want it to resize based on its contents—views don’t do this by default.
In general, if you’re using autolayout in a window, you should never be manually setting the frame of any view. Autolayout overrides any frames you set the every time it’s called, so even if you manage to manually get it working for a second it’ll fail the next time anything triggers a layout.
For views created in code, it's perfectly fine to set their frames as long as their translatesAutoresizingMaskIntoConstraints property is YES (the default, by the way).
However, for a view instantiated in storyboard or a nib, you can not set its translatesAutoresizingMaskIntoConstraints to YES.

How do I hide controls and cause others to shift place in iOS?

I'm programming an app for iOS 7 in Xcode 5.
I have three text boxes at the top of my page, each right above the other. Let's call them topTextBox, middleTextBox, and bottomTextBox.
My intent is that depending on a particular condition, the topTextBox might not be visible (or present, really) when the view loads.
If topTextBox isn't present (or hidden, or whatever) I would like middleTextBox and bottomTextBox to be placed further up the page...as if topTextBox wasn't ever there in the first place (so middleTextBox is in the spot topTextBox used to be in, and bottomTextBox is in the spot middleTextBox was in).
I'm using storyboards with AutoLayout ON. I can't seem to figure out what to do with the constraints for each of the three textBoxes to make this work. Making middleTextBox and bottomTextBox sit higher on the page when topTextBox is hidden doesn't need to be dynamic--I make the decision to show or not show topTextBox in ViewDidLoad()--I just need to get them to show up in the right place depending on my conditions.
Questions:
a. Is making topTextBox.hidden = YES the right way to get topTextBox to not be shown? Or is there some way to make it not noticed by the view at all?
b. What do I do with these constraints on middleTextBox and bottomTextBox to move them up on the page in this condition?
c. Is there anything else I should know to get this to work that I'm not thinking of? Perhaps a better method?
Thanks.
Have you tried just hard coding the frame logic? This could be very effective. All you have to do is conditionally populate an array containing pointers to your text fields and then perform a little frame arithmetic on their y origins. For example, this could easily display all the fields:
NSArray *fields = #[topTextField, middleTextField, bottomTextField];
for (int i = 0 ; i < fields.count ; i ++) {
UITextField *field = fields[i];
[field setFrame:CGRectMake(20.0, 20.0 + (i * 50.0), 280.0, 34.0)];
}
Produces this:
Or at its most basic level, something like this could be used to check whether any of the text fields should/shouldn't be shown, and either hides them, or adjusts their frame accordingly.
BOOL shouldShowTop = YES;
BOOL shouldShowMiddle = NO;
BOOL shouldShowBottom = YES;
NSMutableArray *fields = [NSMutableArray new];
if (shouldShowTop) {
[fields addObject:topTextField];
}else{
[topTextField setHidden:YES];
}
if (shouldShowMiddle) {
[fields addObject:middleTextField];
}else{
[middleTextField setHidden:YES];
}
if (shouldShowBottom) {
[fields addObject:bottomTextField];
}else{
[bottomTextField setHidden:YES];
}
for (int i = 0 ; i < fields.count ; i ++) {
UITextField *field = fields[i];
[field setFrame:CGRectMake(20.0, 20.0 + (i * 50.0), 280.0, 34.0)];
}
Will produce this:
Setting the height constraint of topTextField to 0 will help you to achieve this.
provided all textfields are connected through vertical space constraint
Setting the height of topTextField so that it has zero height could also work for you.
CGRect frame = topTextField.frame;
frame.size.height =0;
topTextField.frame = frame;
This is a good choice should you want to animate its re-appearance later on.
As for constraints, constrain the top of the topTextField to the superview, and then the top of middleTextField and bottomTextField to the bottom of the textfield above.
Do not set a constraint for the height of topTextField, but do set width constraints. You'll need to set the height of topTextField in viewDidLoad:

Searching class in a lot of subviews. Is there a better way?

I'm adding image in subviews of a scrollView, but only to a certain custom class AsyncImageView. This takes some time because there are a lot of subviews and the application has lost it's smooth scrolling.
NSArray *subviewsArray = [[NSArray alloc] initWithArray:[scrollView subviews]];
for (int j = 0; j < [subviewsArray count]; j++) {
for (AsyncImageView *checkView in [[subviewsArray objectAtIndex:j] subviews]) {
if ([checkView isKindOfClass:[AsyncImageView class]]) {
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:checkView];
NSString* urlTextEscaped = [[imageInfo objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:urlTextEscaped];
if (URL)
{
checkView.imageURL = URL;
}
else
{
NSURL *defaultURL = [NSURL URLWithString:#"http://www.domain.com/user_avatar_default.jpg"];
checkView.imageURL = defaultURL;
}
}
}
}
Is there a better way to access the view that I need and add image to it? Some other logic that I haven't think of.
This is the hierarchy of the scrollView
scrollView
|
|
MaseterView
|
|
AsyncImageView - TitleView - OtherImageView
You don't need to create a new array with the subviews of the scroll view before you iterate it. You have 2 general options:
A. Still looping, but not done by you. Only meaningful if you have a single image view or have multiple image views that you want to update individually.
Set the tag of the image view, then use [scrollView viewWithTag:...];
B. If you have multiple image views and you want to update all of them at the same time.
Hold a mutable array property and add the image views to it when you create them.
Other things to think about:
Do you need to update all the image views - are they all on display?
Can you observe the scrolling of the scroll view and only start loads for images when they're on display (and cancel incomplete loads when scrolled off display)?
You can use
UIView *v = [self.containerView viewWithTag:uniqueTagvalue];
set unique tag value for the views you need and fetch it

Resources