I'm trying to create something like this. I've been working with SwiftUI recently so I know I could create that by adding an image, text and button (the I'm flexible text is the label for a button/NavigationLink) to a zstack. but I'm looking around trying to see if there's anyway to do that in UIKit. preferably without using storyboards. I'm open to a cocoapods library or whatever if that's what it takes. I've looked around and explored using SwiftUI to create the desired ZStack and then use it in my UIKit with a UIHostingController but because it involves a button/navigationlink. seeing as how the NavigationLink would require the destination to conform to a View, I wanted to ask around before converting even more of my project to swiftui. I was more hoping this project would be for giving me more experience building views in UIKit without storyboards so I'd prefer to do that instead of using SwiftUI. if that's possible I guess.
I've tried searching around but all my google searches involving UIButtons and images just link to posts about setting the image in a UIButton.
since you wanted to get more experience in creating views using
UIKit
, I've created a view that inherits fromUIView
that you can reuse. There's quite a lot of code to get the same result in UIKit. The code and output are provided below.NOTE: Read the comments provided
Code
Output
Important pointers
You can create the layout using constraints or frames. In case you are using constraints, it is important to set a views
.translatesAutoresizingMaskIntoConstraints
to false (You can read the documentation for it).NSLayoutConstraint.activate([...])
Is used to apply an array of constraints at once. Alternatively, you can use:for individual constraints
E.x., From the leading/left edge, you will need to add a padding of 10 towards the centre of the view or -10 from the right/trailing side towards the centre.