how to make UIVIEW in custom style? - swift

i have to make this type of UIView and there is a image view in the top.
i am new in swift so kindly suggest me the best Answer for it.

I guess that there are many resources on how to design views in swift. But to give you quick advice, the first method is use storyboard and place an image with proper constraints.
The second one is to do it programmatically, by implementing UIView variable and assigning an image to id. Something like this:
private func constructView() {
let myImage = UIImageView(image: "name of image inserted to assets folder in xcode")
myImage.frame = CGRect(x: 200, y: 64, width: 30, height: 30)
container.addSubview(myImage)
}

Related

Button with multiple accessible labels and images in Swift

I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.

How can I use Kingfisher to set the image on a map annotation view?

I want to have images downloaded by kingfisher and set as the image of my AnnotationView. The problem is that Kingfisher only seems to work on UIImageView's and the AnnotationView doesn't seem to expose its.
So one approach would be to use Kingfishers download mechanism to retrieve the image and set it by myself:
KingfisherManager.shared.retrieveImage(with: url) { result in
// Do something with `result`
}
But I'd rather want Kingfisher to set the image because with this approach I'd lose some of Kingfishers features (e.g. placeholder image).
How can I get a hold of the AnnotationView's ImageView?
I've added an imageView to my annotationView. It works with great success.
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 35, height: 42))
annotationView?.addSubview(imageView)

Changing the separator color of UINavigationBar

I’m working on a iOS-App based on Xcode’s “Master-Detail Template” and want to use custom colors for some of the UI elements.
However, I couldn’t find out how to change the right separator of the UINavigationBar:
I’ve already tried to change the backgroundColor of UINavigationBar, UINavigationItem and its titleView but without success.
Would be great if someone has a clue.
EDIT:
I’ve just noted that viewed in vertical mode, it’s the whole separator that I want to ink?
A slight modification of this answer gives you
extension UINavigationBar {
func setRightBorderColor(color: UIColor, width: CGFloat) {
let rightBorderRect = CGRect(x: frame.width, y: 0, width: width, height: frame.height)
let rightBorderView = UIView(frame: rightBorderRect)
rightBorderView.backgroundColor = color
addSubview(rightBorderView)
}
}

UIImageView: applying multiple UIViewContentMode properties to one UIImageView?

How can I add UIViewContentMode.center to this UIImageView while also keeping .scaleAspectFill?
func insertImage() {
let theImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 300))
theImageView.image = #imageLiteral(resourceName: "coolimage")
theImageView.contentMode = UIViewContentMode.scaleAspectFill
view.addSubview(theImageView)
}
Furthermore, can somebody explain to me what the "view" exactly is in the last line "view.addSubview(theImageView)"? Is it the mysterious "view hierarchy" that I read about? Why can't I simply initialize the UIImageView? Why must it be bound to something called "view" that I haven't explicitly created? There is only a UIViewController and a UIImageView so far.
As far as I know, you can't set content mode to both aspect fit and center. However, center will do what aspect fit does providing the image size is smaller than the size of the imageView. If not, use aspect fit. The following code ought to allow you to differentiate between the two:
if (theImageView.bounds.size.width > UIImage(named: "coolimage")?.size.width && theImageView.bounds.size.height > UIImage(named: "coolimage")?.size.height) {
theImageView.contentMode = .aspectFit
} else {
theImageView.contentMode = .center
}
As for the second part of your question, I'll refer you to this thread, which has a fairly comprehensive explanation of UIViewController vs UIView. Hope that helps.

Resize DropDownMenu

I'm new in iOS development.
I use this library (
https://github.com/PhamBaTho/BTNavigationDropdownMenu ) in order to create DropDownMenu to the navigation bar title view. But the problem is that the title is too long and i can't resize the TitleView.
Here is My code and don't work.
let menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, title: self.clausenames.first!, items: self.clausenames)
let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
v.addSubview(menuView)
self.navigationItem.titleView = v
help me please!
BTNavigationDropdownMenu is a library full of private vars.
To change label size (number of lines, width..) you can be meet serious difficulties because the resources are protected and even making extensions to modify his resources throught by checking subviews objects by the kind of classes you must be careful from state of hide/show menu.
Try this:
let menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, title: self.clausenames.first!, items: self.clausenames)
self.navigationItem.titleView = menuView