I have a UIScrollView that works perfectly when I set its content size in the viewDidAppear method, just like this:
-(void)viewDidAppear:(BOOL)animated{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1500)];
}
But what I actually want to do is to calculate the height of my view after I've added all the different subviews in it, and that happens in a method "getAvis".
But when I add these two EXACT SAME LINES, just like this:
-(void)viewDidAppear:(BOOL)animated{
[self getAvis];
}
-(void)getAvis{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1500)];
}
It doesn't work anymore...What is the explanation? I really need to be able to set the content size in a separate method to be able to modify the content size whenever I want!
Thanks
Just like #3CC and #YuviGr pointed out, calling [super viewDidAppear:animated] first in the viewDidAppear method worked...Sometimes life is easy and I am stupid. Thanks guys
Related
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];
}
I have a PopupView that extends UIView. In PopupView I have a UITextView.
When the PopupView show, my UITextView doesn't start at first line (it scroll a little bit to bottom)
So I use the code below to scroll the UITextView to top after PopupView appears
- (void)layoutSubviews{
[super layoutSubviews];
[self.contentTextView setContentOffset:CGPointZero animated:NO];
}
It works well in iOS9 (both device and simulator) but it doesn't work in iOS8
Any idea to fix it.
Any help would be great appreciated
UPDATE
I found that drawRect get called after layoutSubviews and if I setContentOffset:CGPointZero inside it, it will work
-(void)drawRect:(CGRect)rect{
[self.contentTextView setContentOffset:CGPointZero];
}
But I found the purpose of drawRect:
drawRect: - Implement this method if your view draws custom content.
If your view does not do any custom drawing, avoid overriding this
method.
Is it good to use drawRect without layoutSubviews in my case?
According to #longpham instruction, the drawRect() will use GPU so it's not good. Here is the solution that solve my problem
-(void)awakeFromNib{
[super awakeFromNib];
[self layoutIfNeeded]; // call layoutIfNeeded here to make layoutSubviews get called whenever layout change
}
- (void)layoutSubviews{
...
[self.contentTextView setContentOffset:CGPointZero];
}
it worked for me
- (void)drawRect:(CGRect)rect {
self.textView=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self addSubview:self.textView];
}
I have a tableview with frame size of(0, 65, 320, 503).When i navigate to another page then i come back it move down.
add the below line in your - (void)viewDidLoad method.
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.
I hope you found a solution that worked for you. This was happening to me when using AutoLayout and UITableViews combined with the wonderful SWRevealViewController class. Long story short I wasted a bunch of time and eventually found this answer that worked for me:
https://stackoverflow.com/a/23021157/892990
I moved my UITableView to a lower position in the view hierarchy (by putting a dummy 1pt-thick spacer UIView between it and the Top Layout Guide), and now it behaves as expected.
Place this code in view will appear,
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
[self.navigationController.navigationBar setTranslucent:NO];
and also try removing auto layouts.
Hope this helps...
I think you need to set frame of tableview in viewWillAppear method again because some way in your code you r changing the frame so just set it in viewwillappear .
Try this in your viewWillAppear
[your_tableview removeFromSuperview];
[your_tableview setTranslatesAutoresizingMaskIntoConstraints:YES];
your_tableview.frame = CGRectMake(0, 65, 320, 503);
[self.view addSubview:your_tableview];
You could also try this if you don't want to use the header section
your_tableview.tableHeaderView = nil;
if you are using storyboard.
1>set Navigation bar's translucent property to NO. Adjust your subviews frame again because after setting its NO ,the subview will be adjusting with some new frames.
2> In viewDidLoad set the following
self.automaticallyAdjustsScrollViewInsets = NO;
3> its done.
I add a custom view to my UIViewController dynamically. It adds perfectly now, no exception.But the screen is stuck. My scrollview is not functional. I got this problem earlier and people suggested use setContentSize. I did that and it worked fine. Now I have a new scenario.I have 2 screens.In screen A i save values .Then i click a bar button item on screen A and go to screen B. Here in screen B (it's a tableviewcontroller) I select one row and go back to screen A and fill the values accordingly (basically load the saved values that I saved in the beginning of the app). I fill them correctly and also add the scroll view but it is stuck.It doesn't move neither up/down. I have this code written for adding the subview and setting the scroll-view size and frame size.
ITMCustomView *cView = [[ITMCustomView alloc] initWithFrame:CGRectMake(187, 660, 400, 400)
andOrderedItem:#"New"
andTag:self.tagNumber
withFoldArray:self.foldTypeArray
withRollArray:self.rollTypeArray];
cView.tag =self.tagNumber;
NSLog(#"Assigned tag is %d",cView.tag);
cView.delegate = self;
CGPoint scrollPoint = CGPointMake(0.0, (cView.frame.origin.y/500)*400);
[self.scrollView setContentOffset:scrollPoint animated:YES];
[self.scrollView addSubview:cView];
CGFloat scrollViewHeight = 0.0f;
for (UIView *view in self.scrollView.subviews)
{
scrollViewHeight += view.frame.size.height;
}
CGSize newSize = CGSizeMake(320,scrollViewHeight);
[self.scrollView setContentSize:newSize];
NSLog(#"947 the tag number is %d",self.tagNumber);
I check the value of scrollViewHeight..its 3254.000..I changed it to say 4000 as someone suggested increasing height worked. So what am i doing wrong. This question follows another question of mine Navigating back to UIViewController from TableViewController using NSNotifications
If you need more info please ask.Thanks.
Got mine working after I used did appear instead of will appear.
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = self.viewMain.bounds.size;
}
Check the scrollingEnabled property on the scroll view. Unsurprisingly, it needs to be set to YES. If this is the case and it still doesn't work, check if the view (and its superviews) has userInteractionEnabled set to YES as well. In the debugger, you can do po [self.view recursiveDescription] to get the view hierarchy back, that may give you some helpful information as well.
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]