I have a UIStackView in Storyboard with structure as below and want to scroll it to a specific button in viewDidLoad.
This is how I'm scrolling it.
[self.scrollView scrollRectToVisible:button.frame animated:YES];
The problem is, when setting system language to a RTL language (such as Arabic), the direction of UIStackView and button.frame are still left to right in viewDidLoad, so the scroll position is incorrect.
What should I do to fix this?
As you have seen, frame setup is not completed in viewDidLoad.
Move that code to viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.scrollView scrollRectToVisible:button.frame animated:YES];
}
Related
I tried to make floating button on top of uitableview, I already set uitableview to back and the button is in the front on storyboard but when I run the application the table covering the button. How can I make the button to always on front?
Here is the screenshot:
Try to bring your subviews on top of tableview.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view bringSubviewToFront:yourButton];
}
Set autoresizing mask of your buttons from bottom then check. If buttons are not appearing then add this code to in your viewDidLoad method -
[self.view bringSubViewToFront: btn_Cancel];
[self.view bringSubViewToFront: btn_Delete];
I have a Scrollview, it's properties are set in viewDidAppear.
Now when I get to the Scrollview first time there isn't any problem. However I have buttons that are assigned to UINavigationController. So when I press into one of them UINavigationController opens up, when I close the navigation controller, ScrollView does not restore properly. It basically aligns the centre of the screen as previously pressed button location. So if I try to scroll up it does not.
I have tried using this in my viewDidAppear:
scrollView.center = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y);
Which did not quite work. How can I solve this? I am using iOS6.1
Actually I found the answer here:
UIScrollview Autolayout Issue
The exact code that I used is:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
//save the current offset
previousPoint = scrollView.contentOffset;
//set current view to the beginning point
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//retrieve the previous offset
self.scrollView.contentOffset = previousPoint;
}
previousPoint is nothing but a CGPoint variable declared on the implementation.
I've had this problem before too. This answer shows how to overcome this issue.
Basically, you need to set the scrollview's contentOffset appropriately in viewWillAppear: and viewDidDisappear:.
EDIT: Here's another related question that you might find useful, UIScrollview Autolayout Issue.
I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
#synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
Hopefully this is clear. Thanks for all your help!
UPDATE
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
Here's an example of how to set it up in Xcode:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
Here's what happens when I run it in the simulator:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
So, an example to you how to add a Button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
Cheers,
Kel
I want to have several buttons and other objects in a long UIScrollView in my app. In storyboard, I added a UIScrollView to fill the entre view, and then created an IBOutlet in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
#synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
Hopefully this is clear. Thanks for all your help!
UPDATE
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController, UITabBarController, UISplitViewController, or even UIViewController when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
Here's an example of how to set it up in Xcode:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
Here's what happens when I run it in the simulator:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
So, an example to you how to add a Button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
Cheers,
Kel
I've tried many different solutions but none of them are working for me..
I have a horizontal scroll view, now it starts at the left, but how can i make it start at the middle?
I'm using this code right now in the Controller:
- (void)viewDidLoad {
[scroller2 setScrollEnabled:YES];
[scroller2 setContentSize:CGSizeMake(1735, 300)];
[super viewDidLoad];
}
and
IBOutlet UIScrollView *scroller2; in the header file.
I'd made the content in interface builder (.xib file), and i'm using Xcode 4.2!
Thanks in advance!
You are looking for the contentOffset property.
[scroller2 setContentOffset:CGPointMake(appropriateXDisplacement, 0)];
I think you need to call scrollRectToVisible:animated:
try this in viewDidLoad or viewDidAppear or sth. similar.
[scroller2 scrollRectToVisible:CGRectMake(1735/2.0, 300/2.0, 1,1) animated:NO]