swift UITextView setContentOffset works on didappear and not on willappear - swift

I'm trying to automatically scroll my textview at the top before the view loads, so that you can't see it actually moving upward...
i'm trying doing so with
var zeroOffset = CGPoint.zeroPoint
textSpace.setContentOffset(zeroOffset, animated: false)
where textSpace is my UITextView.
when i place this code inside viewDidAppear it works, but the problem is that obviously you can see for a second the text scrolling, and it does't work at all when i place it inside viewWillAppear (viewDidLoad neither if can be of help).
I guess has something to do with the CGPoint that needs the view to be there before calculate the actual point but i'm not sure, is there a solution to this? thanks

In the viewController lifecycle, between viewWillAppear and viewDidAppear, there is a call viewDidLayoutSubviews. It is called after the subviews have been laid out. At that point, you will have everything you need for your call to work, but it will still be before the view appears. So override viewWillLayoutSubviews and place your call there.

Related

Swift Scroll down should make Bar Item disappear

I want that the Bar Button Item diappear when I scroll down. My problem is that I don't know how to detect the scroll? I have tried some code but nothing worked. For example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("123")
}
(Don't work, I call the method in viewDidLoad())
Also I don't know where I have to call the method. In viewDidLoad(), viewDidAppear() or somewhere else? I am new in Swift, sorry.
Does anyone know the answer?
The thing is you are calling it from viewDidLoad, that gets called only once when the view is loaded. You need to place this scrollViewDidScroll
function separately, after viewDidLoad for example, but not inside of it.
Also make sure you implemented UIScrollViewDelegate in your file. Then you can add scrollViewDidScroll method to detect when is the view scrolled and print('123') inside of it.
Also make sure to set your scrollView.delegate = self in your viewDidLoad.

viewWillLayoutSubviews is getting called way too much

In my viewController.view, I have some work done in viewWillLayoutSubviews that organizes which subviews are visible, which to bring to the front, which to hide, etc, if the orientation changes.
But I have buttons on my view that somehow cause viewWillLayoutSubviews to get called every time they are pressed. Why would this be? According to the Apple docs, viewWillLayoutSubviews is only called if your view's bounds change.
The result is that my views are getting re-arranged just from pressing a button, but I'm not rotating the device at all nor am I manipulating view.bounds or view.frame in any way.
You better do one thing andrews take a bool and set it TRUE on button press and check it on layoutSubviews and operate whether it is true or false. You get it what i am trying to do.
Its weird , I wrote a demo just 2 methods , one for button's action and one is - (void)viewWillLayoutSubviews, when I pressed the button, - (void)viewWillLayoutSubviews is not called.Maybe some mistake in your code?

How to reload my UIViewController on click of some buttons?

I'm developing an iPhone app, i have a UIViewController class where some coverflow animations are there and according to design i have some 5 buttons on top of my view each button have an IBAction method on clicking a button i need to display different set of coverflow with different data and the count of coverflow also changes from 1 button action to another. My problem is how can i refresh my page(reload) on click of these buttons, as i know calling explicitly viewDidLoad is a very bad idea, so how can i accomplish this? Any help is appreciated in advance thank you.
The easiest way to get a view to re-draw itself is to call [UIView setNeedsDisplay] or by using [UIView setNeedsDisplayInRect]. This method tells the view that it needs to re-draw itself and it's subviews. However you shouldn't always have to call this. There are other methods that can cause views to redraw themselves. For instance if you add or remove a view to the view hierarchy using [UIView addSubview:] or [UIView removeFromSuperview].
These methods cause the system to call your view's drawInRect: method (assuming you overrode it with custom drawing code). If you did not override this method for custom drawing then it should cause your subviews to receive the same message so that they can get a chance to redraw.
Additional information here. Specifically read the section labeled "The View Drawing Cycle".
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
Why do you want to reload the page?, if it is because you are changing your view and you want it to be redrawn, you can call:
setNeedsDisplay();
But that is often not needed, as whenever a views bounds change that is called automatically for you.
If there's too much other stuff rather than refreshing/reloading the part of your interest in your viewDidLoad method, you can extract the refreshing part into a private method, say methodA. Every time you click your button, you call that methodA. However I think methodA still need call setNeedsDisplay or re-draw the the part of your interest. Hope it helps.
If your point is to refresh "UIViewController", then:
[self viewDidLoad];
Source: How to refresh UIViewController programmatically?
To Refresh your UIViewController, When Clicking the button >>
Add the below code into your UIButton method,
-(IBAction)button:(id)sender{
[self viewDidLoad];
[self viewWillAppear:YES];
}
Now Build and Run Your Code.
Use function viewWillAppear().This function is called everytime the view is called.

I want to redraw a view

I want to redraw a view.
When I touch a button, my view have to redraw itself. (have to call viewWillAppear)
But view's setNeedDisplay method doesn't redraw itself immediately.
Because I have to redraw it immediately, the method is not suitable to me.
I tried to remove all of viewController's view's subviews and to change viewController's
view to other view.
But these didn't work. (don't redraw)
I need your help acutely.
Thank you for your reading.
I think there are some misunderstandings here. Let's set things straight:
viewWillAppear: has nothing to do with the drawing of the view.
It's true that setNeedsDisplay doesn't redraw the view instantly, but we're talking milliseconds. So that's not even relevant.
Since you want viewWillAppear: to be called I'm assuming that what you call redrawing the view really should be referred to as relayouting the view. I assume that what you do in viewWillAppear: is setting the frames of you're views and possibly add/remove and/or show/hide some views.
I suggest that you move that code out of viewWillAppear: and into it's own method that will be called from viewWillAppear: and when you tap the button.
Since you don't provide your viewWillAppear: code this is all based on assumptions, so if my assumptions are wrong, please let me know and please do provide your "redraw" code.
Without more details it's hard to know exactly what you need but I might suggest you look at UIView::setNeedsDisplay. This will in turn cause your views drawRect: to be called where you can update the drawing.
[yourViewName setNeedsDisplay];

determine if uiview is displayed

is there a possibility to determine if an uiview obj is going to be displayed. imagine: you have 2 uiviews in an uiscrollview. now you are going to switch per gesture from the first view to the second. the first view now is NOT in the viewport. now you are going to go back to the first view. and now I want to be notified that this view is in viewport, or is redisplayed. the same has to be for the second view. I have not found any callback or something like this.
You make sure your UiViewController overrides viewWillAppear: (before it appears this method is called) or viewDidAppear: (after this method is called).
See: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear:
That depends what you mean by "switch". If one view is just scrolled out of the visible area of the scrollview, but still remains attached as a subview to it, then you may want to check if the bounds of your view overlap those of the scrollviews visible area.
You could do this by using UIScrollView Delegate's scrollViewDidScroll: method to implement a check for overlaps while the user is scrolling.
If however your view is actually removed from the viewstack, then you may want to subclass UIView and implement willMoveToSuperview: to check if the view has been added to the scrollview again.