Monday, November 25, 2019

Different Bounds between UIScreen.main.bounds and Self.view.bounds

As you know guys, sometimes we want to get height of the screen or the width of the screen. And It's different for every devices. But it's surprisingly different if you call it in ViewDidLayout and ViewDidApper.

Why it can be different ? because in ViewDidLayout the view is not loaded in device screen. And it's bad to get height or width from there. But if you call UIScreen.main.bounds, it will return the right size because UIScreen no need to wait in ViewDidAppear like self.view.bounds.

The user will see some things, it's not good. Like the height change but it's not animation but the bug of waiting the right width. And I encounter that problem, it needs a solution. But my solution before it's not good. Because I just show when the user have arrived and it looks like there's delay displaying the view.

So the solution is initialize the view in ViewDidLayout but using UIScreen.main.bounds to get the right bounds and not self.view.bounds. Self.view.bounds it's good to be used in ViewDidAppear when screen is opened by user.

So this is the code guys, it's simple but really helpful :D.

Tuesday, November 19, 2019

Fixing Keyboard Hide When Reload UITableview iOS swift 5

Hello guys, If you ever worked with UITableview and UITextView or UITextfield. Like chat App or Comment app. You will face the keyboard will hide or in other word the keyboard close to down. And If we other famous apps like Instagram, it's not close but still exist even if we type and send messages.

And In this article I want to share with you guys, how to avoid it so you can easily make the keyboard always opened guys. I found many solution on the internet but some are bad because the keyboard have animation close and open again.

I used UITextview and customize it using GrowingTextView. Because I want to the Textbox change the height when user make a new line. So it will look like Whatsapp chat and It looks good. And I make the textbox above keyboard so it will make the user easily type and send messages.

The point is to make a new class custom and override canResignFirstResponder variable. So we can change it in our own class view controller. And that's good because that's the cause why the keyboard hide and show again. When you set canResignFirstResponder to false. The keyboard will not dismiss even you change page. So we need to set it back to false in viewWillDisappear method to make it close.

And this is the code you're waiting for guys.

Monday, November 11, 2019

Avoid Wrong Height UITableView When Reload and Add New Row

I have a problem where I can't find the solution on the internet like google, stackoverflow and blogs. So I need to trick this so that It work like I want. It's like the bug from iOS because what's supposed to be i'm doing it's the correct way. And the error seems weird because it's only sometimes happened.
I try to find it by way and it takes long time. And finally I got the solution but I think it's not final solution but I hope iOS will fix the issue soon.

So the error it's when you first time entering UITableview.  If you dynamic height like instagram did, you will sometimes get wrong height. But if it's not you are saved. I don't know if this problem only happened to me or not but after a few years expierence. I think this bug happened not only me.

My solution is not the best but I think it's really good to hide the pain because of this annoying bug haha. What I did is to refresh three times refresh when first reload UITableview. So if you have opened the UITableview before don't need to recall it again. Because it will make the memory bad.

Tuesday, November 5, 2019

Add Loading ActivityIndicator in AVPlayer Video iOS Swift 5

This week, I want to show loading after I have a video player that uses AVPlayer library. If you're curious like me. Stay tuned in this blog or if you're in hurry just scrolling on bottom. I must've given the source code. If you're an expert it must be easy to follow.

So if you don't use playback controller library AVPlayerViewController. I mean maybe you use AVPlayer Layer. It's basically it doesn't have playback controls. It's good to use loading Activity Indicator so the user will have a good expierence to operate our app.

So basically the AVplayer itself has a loading indicator beside play/pause button as you can see on above image. But it's not enough for me because it's too little, I need something on center like I do in UIImageview or loading something like loading API.

And the problem with AVPlayer, it doesn't have delegation to perform loading action so we need to use observer to do it. And don't forget to add the ActivityIndicator itself. Define it on public class but it doesn't matter if it is in method class.