I want to put a Stackview of 5 images inside a UIView. Basically what I want is to make a rounded button with a shadow and inside the button 5 different small images horizontally.
What I already have is a viewcontroller with each a declaration and setup function. I am able to make the UIView.
Here is an image of what I am trying to achieve:
How do I solve this?
Some code I already have:
private let btnUIView: UIView = {
let btnUIView = UIView(frame: CGRect(x: 50, y: 50, width: 343, height: 77))
btnUIView.layer.shadowColor = UIColor.black.cgColor
btnUIView.layer.shadowOpacity = 0.5
btnUIView.layer.shadowOffset = .zero
btnUIView.layer.shadowRadius = 3
btnUIView.backgroundColor = .white
btnUIView.translatesAutoresizingMaskIntoConstraints = false
btnUIView.layer.borderWidth = 0
btnUIView.layer.borderColor = UIColor.gray.cgColor
return btnUIView
}()
private let btnStackView: UIStackView = {
let image = UIStackView()
image.translatesAutoresizingMaskIntoConstraints = false
image.distribution = .fillEqually
// image.spacing = 60
return image
}()
func setupBtnView(){
view.addSubview(btnUIView)
btnUIView.addSubview(btnStackView)
NSLayoutConstraint.activate([
btnUIView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
btnUIView.topAnchor.constraint(equalTo: btnText.bottomAnchor, constant: 15),
btnUIView.heightAnchor.constraint(equalToConstant: btnUIView.frame.height),
btnUIView.widthAnchor.constraint(equalToConstant: btnUIView.frame.width),
])
let imgone = UIImageView(image: #imageLiteral(resourceName: "imgtest1"))
let imgtwo = UIImageView(image: #imageLiteral(resourceName: "imgtest1"))
let imgthree = UIImageView(image: #imageLiteral(resourceName: "imgtest1"))
let imgfour = UIImageView(image: #imageLiteral(resourceName: "imgtst1"))
let imgfive = UIImageView(image: #imageLiteral(resourceName: "imgtest1"))
btnStackView.addArrangedSubview(imgone)
btnStackView.addArrangedSubview(imgtwo)
btnStackView.addArrangedSubview(imgthree)
btnStackView.addArrangedSubview(imgfour)
btnStackView.addArrangedSubview(imgfive)
imgone.contentMode = .scaleAspectFill
imgtwo.contentMode = .scaleAspectFill
imgthree.contentMode = .scaleAspectFill
imgfour.contentMode = .scaleAspectFill
imgfive.contentMode = .scaleAspectFill
imgone.clipsToBounds = true
imgtwo.clipsToBounds = true
imgthree.clipsToBounds = true
imgfour.clipsToBounds = true
imgfive.clipsToBounds = true
}
func setupImages(){
view.addSubview(btnStackView)
NSLayoutConstraint.activate([
btnStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
btnStackView.topAnchor.constraint(equalTo: btnUIView.bottomAnchor, constant: 20),
btnStackView.widthAnchor.constraint(equalToConstant: btnStackView.frame.width)
])
What I see in the app, an empty UIView:
This seems pretty straightforward. It took me about two minutes to write the code that gets us this:
That's just a sketchy rendering of what you're after, but it shows that the basic idea is simple enough. All I did was:
Create the outer view, configure it with a rounded border, and add it as a subview.
Create the stack view, configure it with equal spacing etc., and add it as a subview to the outer view.
Create five image views with images (just circles here) and add them as arranged subviews to the stack view.
Literally just 11 lines of code inside my viewDidLoad.
As for your code (which you have now shown), the chief problem is likely that the constraints make no sense. Here's my correction of your code (substituting my own circle image, for test purposes); this is the entire code of my test app view controller:
private let btnUIView: UIView = {
let btnUIView = UIView()
btnUIView.layer.shadowColor = UIColor.black.cgColor
btnUIView.layer.shadowOpacity = 0.5
btnUIView.layer.shadowOffset = .zero
btnUIView.layer.shadowRadius = 3
btnUIView.backgroundColor = .white
btnUIView.translatesAutoresizingMaskIntoConstraints = false
btnUIView.layer.borderWidth = 0
btnUIView.layer.borderColor = UIColor.gray.cgColor
return btnUIView
}()
private let btnStackView: UIStackView = {
let image = UIStackView()
image.translatesAutoresizingMaskIntoConstraints = false
image.distribution = .fillEqually
image.alignment = .center
return image
}()
override func viewDidLoad() {
super.viewDidLoad()
let r = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30))
let im = r.image { _ in UIBezierPath.init(ovalIn: CGRect(x: 1, y: 1, width: 28, height: 28)).stroke() }
let imgone = UIImageView(image: im)
let imgtwo = UIImageView(image: im)
let imgthree = UIImageView(image: im)
let imgfour = UIImageView(image: im)
let imgfive = UIImageView(image: im)
btnStackView.addArrangedSubview(imgone)
btnStackView.addArrangedSubview(imgtwo)
btnStackView.addArrangedSubview(imgthree)
btnStackView.addArrangedSubview(imgfour)
btnStackView.addArrangedSubview(imgfive)
setupBtnView()
}
func setupBtnView(){
view.addSubview(btnUIView)
btnUIView.addSubview(btnStackView)
NSLayoutConstraint.activate([
btnUIView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
btnUIView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
btnUIView.heightAnchor.constraint(equalToConstant: 100),
btnUIView.widthAnchor.constraint(equalToConstant: 300),
])
NSLayoutConstraint.activate([
btnStackView.leadingAnchor.constraint(equalTo: btnUIView.leadingAnchor),
btnStackView.trailingAnchor.constraint(equalTo: btnUIView.trailingAnchor),
btnStackView.topAnchor.constraint(equalTo: btnUIView.topAnchor),
btnStackView.bottomAnchor.constraint(equalTo: btnUIView.bottomAnchor),
])
}
Result:
Related
So, I added some text (UITextView) to my stackView and centered to the top. I also added a UIImageView which would sit nicely under my UITextView. Well it doesn't. For some reason the image covers the text completely. If I delete the image the text comes back up nice on the top center. Played a lot with the stack distribution and alignment but no luck. Not sure what I'm missing :(. Any help is appreciated!
I'm adding both the UITextView and UIIMageView as arrangedSubview to the stack.
Here is my code:
//stack
let stack: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.spacing = 5
stack.distribution = .fillProportionally
stack.alignment = .fill
return stack
}()
//text
fileprivate let title: UITextView = {
let title = UITextView()
title.translatesAutoresizingMaskIntoConstraints = false
title.contentMode = .scaleAspectFit
title.layer.cornerRadius = 10
title.backgroundColor = .darkGray
title.font = UIFont(name: "Megrim-Regular", size: 17)
title.textColor = .white
title.textAlignment = .center
return title
}()
//image
let image: UIImageView = {
let image = UIImageView()
image.image = UIImage(named: "demoPic.jpg")
image.translatesAutoresizingMaskIntoConstraints = false
image.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
return image
}()
Hope this below may help,
I think your issue is relating to constraints applied to the stackview and the holder view. (See below)
Your UI Elements (TextView & Image) code seems to be fine (maybe the image will not be work with 50 width /50 height inside this particular stack view configuration. It will require a different approach IMO.
Nevertheless on my playground in order to see it, I just applied 2 constraints towards my container view in order to see your TextView well above your ImageView as you wanted.
Here is the playground I used to reproduce your issue, you can copy and paste it to see if it fits what you request.
import UIKit
import PlaygroundSupport
/// DEMO VIEW CLASS
final class DemoView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented")
}
}
// YOUR UI CODE
//stack
let stack: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.spacing = 5
stack.distribution = .fillProportionally
stack.alignment = .fill
return stack
}()
//text
fileprivate let title: UITextView = {
let title = UITextView()
title.translatesAutoresizingMaskIntoConstraints = false
title.contentMode = .scaleAspectFit
title.layer.cornerRadius = 10
title.backgroundColor = .darkGray
title.font = UIFont(name: "Megrim-Regular", size: 17)
title.text = "TextView"
title.textColor = .white
title.textAlignment = .center
return title
}()
//image
let image: UIImageView = {
let image = UIImageView()
image.backgroundColor = .red
image.translatesAutoresizingMaskIntoConstraints = false
image.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
return image
}()
// PLAYGROUND DEMO VIEW TO HOLD YOUR STACK VIEW
let demoView = DemoView(frame: CGRect(x: 0, y: 0, width: 350, height: 150))
stack.addArrangedSubview(title)
stack.addArrangedSubview(image)
demoView.addSubview(stack)
demoView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[stackView]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: ["stackView": stack])
)
demoView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[stackView]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: ["stackView": stack])
)
PlaygroundPage.current.liveView = demoView
Results: Your Text View is above the image center (ImageView just have a RED Background here).
My wish is to make centered image(left) and next to it(right) the label.
Without setting an image, there was a perfectly centered titleLabel:
btnWhatsapp.titleLabel?.adjustsFontSizeToFitWidth = true
btnWhatsapp.setTitle("WhatsApp", for: .normal)
Then I added this code to add an image:
btnWhatsapp.setImage(UIImage(named: "phoneIcon"), for: .normal)
btnWhatsapp.imageView?.layer.transform = CATransform3DMakeScale(0.5, 0.6, 0.5)
btnWhatsapp.imageView?.contentMode = .scaleAspectFit
, and this iswhat I got then:
, so the title disappeared.
Maybe the problem is that image uses more space than its actual size(the size shouldnt take more widht and height than the icon size). I saw this when changed images background(should be this much grey color):
btnWhatsapp.imageView?.backgroundColor = .gray
I tried to use the imageEdgeInsets but it is very hard to calculate it to fit perfectly on every iPhone.
This is the Attributes inspector of the button:
You can't set title and image at once by default, nor position them as you describe.
If you need to have a UIButton, I'd recommend to make a UIView (or possibly horizontal UIStackView) with UIImage and UILabel inside, position them with autolayout, then you can add this view to the UIButton as a subview.
let button = UIButton(type: .custom)
button.frame = viewFrame // This is the desired frame of your custom UIView or UIStackView
button.addSubview(customView)
You will be able to position the views easily for all sizes with this approach, but you will probably want to use autolayout in real word app, instead of hardcoded frames.
Example:
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
label.text = "text"
let stack = UIStackView(arrangedSubviews: [image, label])
stack.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
stack.distribution = .fillEqually
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
button.addSubview(stack)
view.addSubview(button)
self.view.addSubview(button)
}
Set your button under your controller class like this:
let imageButton: UIButton = {
let b = UIButton(type: .custom)
b.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
b.layer.cornerRadius = 12
b.clipsToBounds = true
b.translatesAutoresizingMaskIntoConstraints = false
let imageV = UIImageView()
imageV.image = UIImage(named: "yourImage")?.withRenderingMode(.alwaysTemplate)
imageV.tintColor = .white
imageV.contentMode = .scaleAspectFill
imageV.translatesAutoresizingMaskIntoConstraints = false
imageV.widthAnchor.constraint(equalToConstant: 30).isActive = true
let label = UILabel()
label.text = "WhatsApp"
label.textColor = .white
label.font = .systemFont(ofSize: 16, weight: .regular)
let stack = UIStackView(arrangedSubviews: [imageV, label])
stack.distribution = .fill
stack.spacing = 4
stack.axis = .horizontal
stack.translatesAutoresizingMaskIntoConstraints = false
b.addSubview(stack)
stack.heightAnchor.constraint(equalToConstant: 30).isActive = true
stack.widthAnchor.constraint(equalToConstant: 120).isActive = true
stack.centerXAnchor.constraint(equalTo: b.centerXAnchor).isActive = true
stack.centerYAnchor.constraint(equalTo: b.centerYAnchor).isActive = true
return b
}()
Now in viewDidLoad add button and set constraints in your view (in my case on top)
view.addSubview(imageButton)
imageButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
imageButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
imageButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
imageButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
This is the result:
I have a rounded imageView that I use for the profile pic in my app. I use the code several times throughout the app in tableView cells and on different views in view controllers. The profile pic always displays as round. When I use the below code to set the profile pic to round inside the navItem's titleView it only displays as a square.
Why doesn't it display round in the navItem's titleView?
var url: String?//it's already set with some value
override func viewDidLoad() {
super.viewDidLoad(){
setTitleView(urlStr: url)
{
func setTitleView(urlStr: String?){
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.clipsToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.borderWidth = 0.5
imageView.backgroundColor = UIColor.white
if let urlStr = urlStr{
let url = URL(string: urlStr)
imageView.sd_setImage(with: url!)
}
containerView.addSubview(imageView)
imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
navigationItem.titleView = titleView
}
Looks like imageView.layer.cornerRadius = imageView.frame.size.width / 2 is set to 0. imageView.frame.size.width is equal to 0 here.
Instead of let imageView = UIImageView(), you can predefine the frame when creating the imageView with let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
I am new to Swift, and I have been working on a project and ran into a peculiar issue that I fixed, but wasn't quite sure why the solution worked.
I have a class UserViewController where I do something like:
class UserViewController: UIViewController {
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "profilepic")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30)
imageView.layer.cornerRadius = 0.5 * imageView.bounds.size.width
imageView.clipsToBounds = true
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
view.addSubview(profileImageView)
profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.topAnchor.constraint(equalTo:topLayoutGuide.bottomAnchor, constant: 24).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfilePicture))
profileImageView.addGestureRecognizer(tapGestureRecognizer)
profileImageView.isUserInteractionEnabled = true
}
I was trying to add a tap gesture recognizer to my UIImageView. What I was doing before was actually adding the recognizer in my initialization for profileImageView like this:
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "profilepic")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30)
imageView.layer.cornerRadius = 0.5 * imageView.bounds.size.width
imageView.clipsToBounds = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfilePicture))
profileImageView.addGestureRecognizer(tapGestureRecognizer)
profileImageView.isUserInteractionEnabled = true
return imageView
}()
When I was doing this, the recognizer was not working (I would tap on the image and nothing would happen, the handler function was never invoked). When I pulled out the three lines that created/added the gesture recognizer to my image view, and moved them into setupView() the tap was recognized, and everything worked as expected.
For the betterment of my understanding, why is this the case? Why can't I add the gesture recognize in the initialization of my image view and have it work?
Thanks in advance.
You were modifying the not-yet returned variable in these two lines in your old code:
profileImageView.addGestureRecognizer(tapGestureRecognizer)
profileImageView.isUserInteractionEnabled = true
Replace profileImageView with imageView and it will work.
This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Closed 5 years ago.
import UIKit
class MenuBar: UIView{
let buttonWidth = 50
let friendsButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Friends Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
creating a friends button
let circleButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Small Circle Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
creating a circle button
let profileButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Profile Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
creating a profile button
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])}
The last line is the line that gives me the error: cannot use instance member 'profileButton within property initializer
The important part of the error message you got is the second part which unfortunately you omitted. It is property initializer is initialized before self.
The next error you'll get after rewrite without property initalizer pattern would be Expected declaration. That means the code segment need to be inside a function. Like viewDidLoad() for example.
Thus, for it to work, your code need to be modified to something similar to:
import UIKit
class MenuBar: UIView {
func createStackView() -> UIStackView {
let buttonWidth = 50
let friendsButton = UIImageView()
friendsButton.translatesAutoresizingMaskIntoConstraints = false
friendsButton.image = #imageLiteral(resourceName: "Friends Button")
friendsButton.contentMode = .scaleAspectFill
friendsButton.widthAnchor.constraint(equalToConstant: 50)
friendsButton.heightAnchor.constraint(equalToConstant: 50)
let circleButton = UIImageView()
circleButton.translatesAutoresizingMaskIntoConstraints = false
circleButton.image = #imageLiteral(resourceName: "Small Circle Button")
circleButton.contentMode = .scaleAspectFill
circleButton.widthAnchor.constraint(equalToConstant: 50)
circleButton.heightAnchor.constraint(equalToConstant: 50)
let profileButton = UIImageView()
profileButton.translatesAutoresizingMaskIntoConstraints = false
profileButton.image = #imageLiteral(resourceName: "Profile Button")
profileButton.contentMode = .scaleAspectFill
profileButton.widthAnchor.constraint(equalToConstant: 50)
profileButton.heightAnchor.constraint(equalToConstant: 50)
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])
return stackView
}
}