Karhin’s Blog
Apps, Design and Music
Later ↑

How to place a view in the UINavigationBar and increase its height

You can see an expanded UINavigationBar with additional UIViews located below the title in many native Apple applications.

Calendar and Fitness

There is a very simple way to add your own UIView as a pallet in UINavigationBar using private API, which was discovered by @SebJVidal. You can take the source code from the tweet and close the article. Just don't forget to obfuscate the private selectors before the review 😁

But there is another way. In the latest update of Artykul, in the new Links section, I recreated similar behavior without using a private API and at the same time, I didn't have to create my own UINavigationBar from scratch.

Before coming to a solution, I tried making subclasses of UINavigationBar, trying to add UIView directly, and some other things that either don't work or break the smooth transitions between UIViewController. The main conclusion I reached is not to even try to touch the navigation bar.

Sometimes the simplest solutions, which lie right on the surface, are hard to see at first. This is one of them.

The main trick is not to try to extend what cannot be extended by design, but to recreate this behavior. Fortunately, it's fairly simple to do so.

All the source code is available in this repository. I will only focus on a few key points that explain what is happening.

Visual

You need to make the UINavigationBar completely transparent. This is best done through UINavigationBarAppearance, if you don't want to break transparency in the UINavigationController stack.

let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()

navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
navigationItem.compactAppearance = appearance
navigationItem.compactScrollEdgeAppearance = appearance

Now we can start creating our PalletView. The original naming from the private API looks strange, so let's call it a pallet.

We need to recreate the original border (or shadow) and background blur. The color of the original “shadow” can be obtained from the already familiar UINavigationBarAppearance. The height of this shadowView should be equal to one physical pixel: 1.0 / UIScreen.main.scale.

private let shadowView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    
    let navigationBarAppearance = UINavigationBarAppearance()
    navigationBarAppearance.configureWithOpaqueBackground()
    view.backgroundColor = navigationBarAppearance.shadowColor
    
    return view
}()

The background is simpler. You can use anything, but to closely match the original UINavigationBar, it's best to use this effect.

let standardVisualEffect = UIBlurEffect(style: .systemThinMaterial)

In the original implementation, it's easy to notice that the shadow and background are hidden when we're at the very top of the UIScrollView. Therefore, it's necessary to provide two methods that will be used to respond to scrolling.

func scrollEdgeAppearance() {
    shadowView.alpha = 0.0
    visualEffectView.effect = nil
}

func standardAppearance() {
    shadowView.alpha = 1.0
    visualEffectView.effect = standardVisualEffect
}

Layout

A UILabel is used as the contentView, but you can replace it with anything you like. The most important aspects are:

  • The visualEffectView should match our PalletView.
  • The shadowView needs to be attached to the bottom of the PalletView.
  • The top anchor of the PalletView should correspond to view.topAnchor.
  • The bottom anchor of the PalletView should correspond to the bottom anchor of the contentView.
  • The top anchor of the contentView should correspond to view.safeAreaLayoutGuide.topAnchor.
// View Controller

palletView.leftAnchor.constraint(equalTo: view.leftAnchor),
palletView.topAnchor.constraint(equalTo: view.topAnchor),
palletView.contentView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
palletView.rightAnchor.constraint(equalTo: view.rightAnchor)
// PalletView

contentView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: 0.0),
contentView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: 0.0),
self.bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: 8.0)

Since we have PalletView placed on top of a UIScrollView or UICollectionView, don't forget to add an inset to it equal to the height of PalletView minus the safeArea. Also, remember about the scroll indicators.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionView.contentInset.top = palletView.frame.height - view.safeAreaInsets.top
    collectionView.verticalScrollIndicatorInsets.top = collectionView.contentInset.top
}

Scroll

Now, all that's left is to animate the palletView. Here, we'll need the two methods we added earlier. Just remember to add the height of palletView to the contentOffset, since we manipulated the inset.

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let scrollContentOffset = scrollView.contentOffset.y + palletView.frame.height
    if scrollContentOffset > 0.0 {
        UIView.animate(withDuration: 0.100, animations: {
            self.palletView.standardAppearance()
        })
    } else {
        UIView.animate(withDuration: 0.100, animations: {
            self.palletView.scrollEdgeAppearance()
        })
    }
}

There is still some room for improvement here. In the original UINavigationBar implementation, the animation is not done through a timer, but rather based on the scroll value within a very small range (approximately 4 points). So, the code should look something like this.

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let scrollContentOffset = scrollView.contentOffset.y + palletView.frame.height
    if scrollContentOffset < 0.0 {
        self.palletView.setVisibilityPercentage(to: 0.0)
    } else if scrollContentOffset < 4.0 {
        self.palletView.setVisibilityPercentage(to: scrollContentOffset / 4.0)
    } else {
        self.palletView.setVisibilityPercentage(to: 1.0)
    }
}

You can try implementing this yourself, but it will require diving into the wonderful world of UIVisualEffectView if you plan to use the blur effect, but that's a whole different story 😁

Result

The entire source code with an example is available in the repository.

Don't forget to follow me on X/Twitter and Telegram.

Ekran is available on the App Store

Ekran is our new application that allows you to turn even the most unsuccessful screenshot or screen recording into something you wouldn't be embarrassed to share on social networks or post on your blog.

How much time has been wasted manually fitting screenshots to center them perfectly within the content? How many minutes are spent finding the right device frame when preparing a post or images for websites? And how much time must be dedicated to doing this in a video editor, dealing with masks and worrying about pixel-perfect placement?

Now, not even a minute is needed, thanks to Ekran. We've made the app as straightforward as possible. And it's already available for iOS, iPadOS, and macOS. Moreover, there are no unpleasant subscriptions involved. We've created it as if it were for ourselves.

I'm sorry, I've always wanted to do this, throughout the development process 😅

The main features:

  • Videos, and everything operates super fast without even heating up the phone, even on very old iPhone models.
  • Cross-platform support. Everything as we like it.
  • Hiding of banking cards, phone numbers, and email addresses.
  • A hundred ways to insert an image into the application, to make it as easy as possible.
  • Automatic balancing of screenshots.
  • Margins, padding, shadows, rounded corners, fun backgrounds, and that's all.

And most importantly, there are no subscriptions, and practically all features are free. If you want more, it's just a one-time payment of $20. And that covers all three applications for all your favorite devices. We've looked at the competitors; try to find something like it.

The application can be downloaded from the App Store.

3 Ways to Play Xbox on a Mac

There are a thousand and one reasons why this might be useful: the TV is occupied or you don't have one at all, poor eyesight or something else. In any case, Macs do have good displays, but unfortunately, there is no simple way to connect a console directly.

Remote Play

As of the end of 2023, Microsoft still doesn't have an official Remote Play app for macOS, but they do have one for iPad and iPhone. I don't understand why they haven't ported it yet: there's no objective reason.

Fortunately, there are third-party apps, such as OneCast. I've used it for several months. Currently, it costs about $30 for a one-time purchase.

It works roughly the same way as Remote Play in the official app. Sometimes you connect once and see Full HD with almost no lag, and sometimes you're watching a pixelated slideshow. I never quite understood how it works under the hood and how it's related to the phases of the moon, but I did manage to play several games on it.

HDMI Converter

There's this magical thing called an "HDMI Capture Card.” It's a device that connects to the computer via Type-C and converts the HDMI signal into something that the computer recognizes as a webcam.

Such devices range from $10 to $50. I'll admit I made a mistake and ordered an adapter from Sandberg, which converts to Full HD at 30Hz (funny that it's cheaper than OneCast). Theoretically, you can find devices with better specs, but they might be a bit more expensive.

To play, I use OBS. I tried with QuickTime, VLC, and some other apps, but they created noticeable lag. In an ideal world, this should have been the perfect solution, but it has its drawbacks.

  • The picture quality and stability immediately outshine Remote Play.
  • The adapter creates a slight delay that you need to get used to. It doesn't interfere with playing games like Cyberpunk or Starfield.
  • Pay close attention to the output image specifications, not like I did.
  • This setup works for iPads with a Type-C port (iOS 17 or later) and an app like Orion.

Portable Display?

Okay, it seems playing Xbox via a Mac isn't a great idea. Another option: buy a portable display. I've seen 13 and 15-inch portable IPS Full HD displays on the market. They're priced similarly to a budget Samsung TV, are space-efficient, and weigh around 800 grams.

In short, the best option: get rich, buy a cinema, and play Xbox there instead of dealing with these alternatives. Don't forget to subscribe to my Telegram channel. I'd skip Twitter since Musk might soon ruin it completely.

Why Should Lightning Go Away

There's no secret that over the past year, Apple has considered removing the Lightning port from iPhones and accessories. Before the presentation, there were many discussions among Apple's sectarians.

Should the European Union influence major Silicon Valley companies at all? Was Lightning the best connector in the world? Is Type-C evil? And many other strange questions.

Most of the arguments in favor of a proprietary connector are sheer sectarianism. I want to break down a bit why Lightning should have disappeared a long time ago.

Peripherals

Let's start with the fact that Lightning was a good form factor for its time. At that time, there was no Type-C specification, but there was an awful Micro USB, which can still sometimes be found in devices because it's cheap. Besides being one-sided, it constantly broke.

Now almost all technology is released with a Type-C connector. One cable is enough to charge a computer, connect a monitor Thunderbolt cord is needed, PlayStation or Xbox controller, headphones (except those from Apple, of course), iPad, or camera.

Currently, only the iPhone, AirPods, keyboard, and mouse require their proprietary wire.

It also works the other way around. You can't just connect an SSD to an iPhone to transfer a 30-minute 4K video. You can't directly connect a camera to transfer video from it to your phone. You can't connect your phone to a TV if it doesn't support AirPlay or if the wireless network quality is poor.

Almost any case of connecting something to an iPhone ends with it being impossible to do so. Meanwhile, I've been able to do all this with my iPad for a couple of years.

Type-C Is Unreliable and Some Different Cords

I'll just remind you that Type-C is just a form factor for USB, which can support different specifications. It might not even transfer data, just provide power to the device.

Lightning is the same. But the joke is that it only supports the outdated and slow USB 2.0. And there are also Lightning cords from China that can only charge the phone.

Myths about Type-C being unreliable compared to Lighting are mostly nonsense. The pins in Lighting can break just as easily, and the connector itself oxidizes over time. If it's somewhat more reliable, it doesn't outweigh all the benefits of a universal connector.

To cover 99% of the use cases of any Type-C connector, buy quality cords. A good cable isn't cheap, and the price starts at $40. A good cable usually has a lightning icon, indicating that the cord supports Thunderbolt. Such cords can even be used to connect monitors.

The only reason why Lighting hasn't disappeared yet is money. Apple made money on this connector; manufacturers must pay Apple to do something with this connector.

Just a reminder of the glorious times when phones didn't have a 3.5mm jack and headphones from various manufacturers were tightly tied to the device because of a custom connector. Sounds cool? Apple still sells headphones with Lightning when all other wired headphones are with Type-C. Just like 20 years ago.

UPD: I wrote this post before the presentation and unexpectedly guessed all the points that were made. Now they will be selling EarPods with a Type-C connector. Are you still against EU regulation?

Apple Museum in Poland

As you can guess, I'm a fan of computers and Apple. One morning, we thought about where to go, and Kate read about the Apple museum in Warsaw. My first thought was something like oh-my.

This museum is the largest in the World. I think it's a fun fact. Steve Wozniak – one of the founders of Apple – has Polish roots. The family name also hints about it.

Cześć, Hello, Bonjour! The ticket costs $12.

The museum is in Fabryka Norblina in the center of Warsaw. It's a factory with 200 years of history. There are more than 1600 exhibits related to the history of Apple. Do you know about Apple printers or cameras?

Apple I

Apple I was the first computer developed by Apple in 1976. The introductory price was $666.66. There are no more than 50 computers in the world nowadays. If you are lucky enough, you can find one at the auction for $500.000.

It's a replica of the original computer, but you can see real Wozniak's sign on the user manual and the motherboard.

Apple II and friends

Apple II is a legend in the computer world because it's one of the first personal computers and the most produced and cloned computer.

Timeline of Apple II family, Wikipedia.

The series was started in 1978 and discounted in 1993. There are many-many modifications and many-many clones. Some sources say that around 200 clone models were produced.

Almost modern Macs

Zoomers don't know about them. Boomers say that they were the best computers and the grass was green. For me, it's just computers.

I didn't know about enterprise solutions like Workgroup Server before. The series of Workgroup Servers was started in 1993 and discounted in 2003. Also, "Studio Display" is reused title from 1999.

Notebooks

Newton, colorful iBook, PowerBook? Black MacBook from plastic?

Duo Dock II is one of the most strange devices, in my opinion. The docking station turns a connected PowerBook Duo into a full-featured desktop Macintosh.

Printers

Apple produced printers and print paper from the 1980s to the end 1990s. They can be divided into three groups: dot matrix, laser, and thermal inkjet.

Some weird things from Apple

Analog Apple Watch, digital camera QuickTake, video conference camera, the repair suitcase. Wow!

Posters

Software, guides, and other boxes

Do you remember software boxes, CDs, and tapes? I think it's a kind of forgotten art. Some of the examples were very unexpected for me. Some of them look very fresh even today.

Bonus videos

I played with old-school iMacs produced at the start of the 2000s. Sometimes I see opinions like old macOS was better and faster. So, you can check the genie effect on the original iMac PowerPC!

Really don't understand why modern OS are so silent. Just listen to these sounds. I think they are fun and cute.

Conclusion

Must see it in Warsaw. It's a nice place if you love the history of computers or are a tech lover. Exhibition changes time-to-time. I visited the museum at the end of 2022.

Don't forget to subscribe to my Telegram channel, Instagram, and Twitter.

Earlier ↓