I have a UICollectionView that is used to simulate the new calendar in iOS 7. This collection view is inside a controller that has a selectedDate property. Whenever the selectedDate property is set the collection view should scroll to the date in the collection view.
The calendar controller's viewWillAppear also ensure the selected date is visible because this controller is cached and reused.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.calendarView scrollToDate:[self selectedDate] animated:NO];
}
The problem is that the VERY first time the calendar controller is shown the scroll does not work. The contentOffset of the collection view is not updated.
My current workaround is to schedule the scroll to occur on the next run loop using
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void)
{
// Scroll to the date.
});
It looks like when the UICollectionView is not in a window you cannot scroll. Scheduling the scroll to happen on the next run loop ensure that the view has been added to the window and can be properly scrolled.
Has anyone else experienced this issue and what their workarounds?
you can always force auto-layout to layout.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.view.layoutIfNeeded()
self.collectionView.scrollToItemAtIndexPath......
}
If you are using auto layout, the issue may be that the constraints haven't set the frames yet. Try calling the scrollToDate: method in viewDidLayoutSubviews (without dispatch_after).
#interface CustomViewController ()
#property (nonatomic) BOOL isFirstTimeViewDidLayoutSubviews; // variable name could be re-factored
#property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
#end
#implementation CustomViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.isFirstTimeViewDidLayoutSubviews = YES;
}
- (void)viewDidLayoutSubviews
{
// only after layoutSubviews executes for subviews, do constraints and frames agree (WWDC 2012 video "Best Practices for Mastering Auto Layout")
if (self.isFirstTimeViewDidLayoutSubviews) {
// execute geometry-related code...
// good place to set scroll view's content offset, if its subviews are added dynamically (in code)
self.isFirstTimeViewDidLayoutSubviews = NO;
}
bilobatum's answer is correct!
I'm writing this because I don't have reputation to comment... :/
I tried bilobatum's answer in my project, and it worked perfectly!
My code:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
if (currentOffset.y != 999) {
[collectionView setContentOffset:currentOffset animated:NO];
}
}
currentOsset is a CGPoint initialized with x = 0 and y = 999 values (CGPoint currentOffset = {0,999};)
In the viewWillDisappear method I save the collectionView's contentOffset in the currentOffset.
This way if I navigate to the controller that has the collectionView and I navigated to there before, I will always have the last position.
The code that will work for you:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self.calendarView scrollToDate:[self selectedDate] animated:NO];
}
Thank you bilobatum for the answer!
Using -viewDidLayoutSubviews created an infinite loop that made the solution too complicated.
Instead I just added a small delay to let the constraints be created before the scrolling:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self.scheduleDate isThisWeek]) [self.calendarLayout performSelector:#selector(scrollToCurrentTime) withObject:nil afterDelay:1];
}
Related
I have a screen, which contains multiple UIImages (their amount and size are known only at runtime, so i add them programmatically) and some fixed buttons below these UIImages.
How to make buttons display certainly under all Images?
I've tried
1.) Put Buttons and Images into 2 separate views, and then add constraint between them. No result, buttons are hidden behind images.
2.) Put buttons into separate view and set constraint in code, (tried both viewDidLoad and viewDidAppear). Constraint is set between container view and top of the screen, depending on size and amount of images.
Example of code:
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSInteger totalImages = [self.object.fullphotos count];
self.labelsTopConstraint.constant = totalImages*(imageHeight + 20) + 10;
}
In case 2 buttons are positioned right, but don't respond to touches.
How should I layout everything correctly?
Thanks in advance!
Take a Tableview for those images and add buttons in a last cell.
The best way is creating a Object with a refresh method that can be called in viewDidAppear
MyObject.h
#interface MyObject : UIViewController
#property (nonatomic,strong) UIImageview *img;
#property (nonatomic,strong) UIButton *btn;
- (void) refresh;
in MyObject.m
- (void)viewDidLoad {
[self.btn addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) refresh {
//make your settings here
}
-(void)myMethod {
//your button action here
}
Then in your controller if you have your objects in an NSArray:
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
for (MyObject *myObj in objectsArray) {
#autoreleasePool {
[myObj refresh];
}
}
}
have been looking around, but I have not found an answer that helped me out. I am wondering how I can keep the scroll position in a scrollview when I move forwards and backwards in a navigation controller. Currently, when I go to another view in the navigation controller, the scroll view resets and is at the top when I go back. Any code would be greatly appreciated. Sorry if my explanation was not good enough. So far this is what I have, I know that this won't work, but what should I do?
- (void)viewWillAppear:(BOOL)animated {
scrollView.contentOffset.y = scrollSave;
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
CGFloat scrollSave = scrollView.contentOffset.y;
[super viewWillDisappear:animated];
}
The code you have above shouldn't even compile. You could save this scroll position in another variable, but generally the scroll position shouldn't be resetting itself anyway. You should remove any code that is trying to manipulate the content offset at all, and see if it restores it to the correct scroll position upon returning to the view.
With your current approach, scrollSave is a local variable declared in viewWillDisappear so you cannot access the value in viewWillAppear. You will need to create an instance variable or a property. (If the view controller is still in memory, it should maintain the contentOffset so this might not be necessary)
#interface MyViewController ()
#property (nonatomic) CGPoint scrollSave;
#end
#implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
[scrollView setContentOffset:self.scrollSave];
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
self.scrollSave = scrollView.contentOffset;
[super viewWillDisappear:animated];
}
Now there is an issue with persistence. If the you navigate back in the navigation controller, this view controller will be released from memory. If you need to save the users position then I would suggest using NSUserDefaults.
#interface MyViewController ()
#property (nonatomic) NSUserDefaults *defaults
#end
#implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
CGPoint scrollSave = CGPointFromString([_defaults objectForKey:#"scrollSaveKey"]);
scrollView.contentOffset = scrollSave;
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[_defaults setObject:NSStringFromCGPoint(scrollView.contentOffset) forKey:#"scrollSaveKey"];
[_defaults synchronize];
[super viewWillDisappear:animated];
}
Kyle's answer should get you storing the variable okay, but in regards to assigning the contentOffset.y, trying animating the scrollview.
[scrollView scrollRectToVisible:
CGRectMake(scrollView.frame.origin.x, scrollSave.y, scrollView.frame.size.width, scrollView.frame.size.height)
animated:NO];
That code would go into viewWillAppear.
I have UICollectionView for the user to select images from a grid. Once a cell is selected, the view controller is released, including the UICollectionView.
I'd like to remember where the user was last time they used the UICollectionView and automatically scroll to that location when the view controller is loaded again.
The problem I'm encountering is when this can be done. I'm assuming I need to wait until the UICollectionView has been fully loaded before executing scrollToItemAtIndexPath:atScrollPosition:animated:.
What's the preferred way to determine when the view controller and UICollectionView are fully laid out?
I spent some time exploring solutions. #Leo, I was not able to scroll in viewDidLoad. However, I was able to achieve good results keeping track of the state of the view life cycle.
I created a constant to remember the content offset. I used a constant so it would retain it's value between loads of the VC. I also created a property to flag when it was okay to scroll:
static CGPoint kLastContentOffset;
#property (nonatomic) BOOL autoScroll;
#property (weak, nonatomic) IBOutlet UICollectionView *collection;
The life cycle code I used:
- (void)viewDidLoad
{
[super viewDidLoad];
self.autoScroll = NO; // before CollectionView laid out
}
- (void)viewDidDisappear:(BOOL)animated
{
kLastContentOffset = self.collection.contentOffset;
[super viewDidDisappear:animated];
}
- (void)viewDidLayoutSubviews
{
if (self.autoScroll) { // after CollectionView laid out
self.autoScroll = NO; // don't autoScroll this instantiation of the VC again
if (kLastContentOffset.y) {
[self.collection setContentOffset:kLastContentOffset];
}
}
During the layout of the collectionView I set the flag indicating that the next viewDidLayoutSubviews should auto scroll:
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
self.autoScroll = YES;
// return count here
}
The important part was saving the content offset when my view disappeared. The constant is not remembered between launches of the application which what I wanted and the reason for not saving it in preferences.
Seems like there has to be a more elegant solution but this works well.
I would like to know if a cell is being dragged (via the grip on the right side when a UITableView is in editing mode). I would like to change the background while it is in that state.
There is no callback unfortunately, but since the default behaviour is to adjust the 'alpha' value of the UITableViewCell you can check for that in '- (void)layoutSubview' of your UITableViewCell subclass.
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.alpha < 0.99) {
// we are most likely being dragged
} else {
// restore for not being dragged
}
}
Note that this is a bit of a hack. I noticed that this doesn't work when the table view's separatorStyle is set to 'UITableViewCellSeparatorStyleNone'.
I don't know if this is possible in the framework. But if it's not, you can try to override touchedMoved method in an UITableViewCell subclass?
You might then be able to also retrieve the state of your tableView (editing or not) in your UItableViewCell's custom subclass.
It is possible, yes!
In ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UITableView * myTable;
IBOutlet UIScrollView * myScroll;
}
In ViewController.m
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myScroll = myTable;
myScroll.delegate = self;
}
#pragma Mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(#"%f",scrollView.contentOffset.y);
}
That is it.
It's possible yes! But you have to keep track on the states yourself in the view controller. Define a member
BOOL tableDrag = NO;
In the method
scrollViewWillBeginDragging
you set tableDrag = YES; And in the method
scrollViewDidEndDragging
you set tableDraw = NO;
Now in the scrollViewDidScroll delegate method you can set the background.
If you have several scrollers then tag the scroll view so that you know which one is dragging.
Hope it helps!
I can't figure out what the problem is here. I have a very simple UIViewController with a very simple viewDidLoad method:
-(void)viewDidLoad {
NSLog(#"making game view");
GameView *v = [[GameView alloc] initWithFrame:CGRectMake(0,0,320,460)];
[self.view addSubview:v];
[super viewDidLoad];
}
And my GameView is initialized as follows:
#interface GameView : UIView {
and it simply has a new drawRect method:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
NSLog(#"drawing");
}
In my console, I see "making game view" being printed, but "drawing" never is printed. Why? Why isn't my drawRect method in my custom UIView being called. I'm literally just trying to draw a circle on the screen.
Have you tried specifying the frame in the initialization of the view? Because you are creating a custom UIView, you need to specify the frame for the view before the drawing method is called.
Try changing your viewDidLoad to the following:
NSLog(#"making game view");
GameView *v = [[GameView alloc] initWithFrame:CGRectMake(0,0,320,460)];
if (v == nil)
NSLog(#"was not allocated and/or initialized");
[self.view addSubview:v];
if (v.superview == nil)
NSLog(#"was not added to view");
[super viewDidLoad];
let me know what you get.
Check if your view is being displayed. If a view is not currently on screen, drawRect will not be getting called even if you add the view to its superview. A possibility is that your view is blocked by the some other view.
And as far as I know, you don't need to write [super drawRect];
Note that even if viewDidLoad is called on a view controller it doesn't necessarily indicate the view controller's view is displayed on screen. Example: Assume a view controller A has an ivar where a view controller B is stored and view controller A's view is currently displayed. Also assume B is alloced and inited. Now if some method in A causes B's view to be accessed viewDidLoad in B will be called as a result regardless whether it's displayed.
If you're using a CocoaTouch lib or file you may need to override the initWithCoder method instead of viewDidLoad.
Objective-C:
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
//Do Stuff Here
}
return self;
}
I had a view that was offscreen, that I would load onscreen and then redraw. However, while cleaning up auto-layout constraints in XCode, it decided my offscreen view should have a frame (0,0,0,0) (x,y,w,h). And with a (0,0) size, the view would never load.
Make sure your NSView has a nonzero frame size, or else drawRect will not be called.