I cannot change the size of the text in a programmatically created UILabel.
Here's a screen grab:
Here's the class definition:
class myView: UIView {
let theLabel = UILabel()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.theLabel.text = "Text"
self.theLabel.backgroundColor = UIColor.lightGray
self.backgroundColor = UIColor.blue
self.theLabel.adjustsFontSizeToFitWidth = true
self.theLabel.textAlignment = .center
self.theLabel.numberOfLines = 1
self.theLabel.minimumScaleFactor = 0.1
self.theLabel.font = UIFont( name: "System", size:160)
self.theLabel.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.theLabel)
let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0)
let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)
let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)
let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint])
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
I've tried changing the font size. I've set adjustsFontSizeToFitWidth to true and set minimumScaleFactor. The text size is always the same. I expect it to fill the label area, the grey box.
How do I get this to work?
try adding this line after you set the font:
self.theLabel.font = theLabel.font.withSize(45)
Your code works as expected, without any change.
CustomView1NoXIB.swift
import UIKit
class CustomView1NoXIB : UIView {
let theLabel = UILabel()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSubviews()
}
func initSubviews() {
self.theLabel.text = "Hello"
// self.theLabel.text = "Hello World Big Text Becomes Small"
self.theLabel.backgroundColor = UIColor.lightGray
self.backgroundColor = UIColor.blue
self.theLabel.adjustsFontSizeToFitWidth = true
self.theLabel.textAlignment = .center
self.theLabel.numberOfLines = 1
self.theLabel.minimumScaleFactor = 0.1
self.theLabel.font = UIFont( name: "System", size:160)
self.theLabel.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.theLabel)
let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0)
let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)
let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)
let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint])
}
}
CustomView1ViewController.swift
import UIKit
class CustomView1ViewController : UIViewController {
#IBOutlet weak var customView1: CustomView1!
#IBOutlet weak var customView1NoXIB: CustomView1NoXIB!
override func viewDidLoad() {
super.viewDidLoad()
}
}
Thanks,
Sriram
Related
So I have created a Progress Indicator View that I am showing on API calls. I have created a custom UIView Class for it.
Now, everything works fine. But the position of view should be in centre but it's not.
I think I have the constraints right but still its not working.
Here is my code:
import Foundation
import UIKit
import UICircularProgressRing
import HGRippleRadarView
class ProgressIndicator : UIView {
#IBOutlet weak var contentView : UIView!
#IBOutlet weak var progressView : UICircularProgressRing!
#IBOutlet weak var logoContainerView : UIView!
#IBOutlet weak var rippleView : RippleView!
static let shared = ProgressIndicator()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("ProgressIndicator", owner: self, options: nil)
addSubview(contentView)
}
public func show(controller : UIViewController) {
setupLoadingView(controller : controller)
}
public func hide() {
removeLoadingView()
}
private func setupLoadingView(controller : UIViewController) {
controller.view.addSubview(self)
// adding contrints on main view
let leadingConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
controller.view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
// adding constraints on content view
let leadingConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
let trailingConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
let topConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
let bottomConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
self.addConstraints([leadingConstraint1, trailingConstraint1, topConstraint1, bottomConstraint1])
self.setNeedsLayout()
self.reloadInputViews()
self.layoutIfNeeded()
}
}
Here is the result m getting:
And here is the xib file screenshot
You have added constraints but didnt set translatesAutoresizingMaskIntoConstraints to false.
self.translatesAutoresizingMaskIntoConstraints = false
self.contentView.translatesAutoresizingMaskIntoConstraints = false
I have written a custom navigationbar but habe the problem, that the text inside my buttons is not perfectly aligned. For example see the + Button.
Any ideas how to improve this?
import UIKit
class DashboardNavbar: UINavigationBar {
var titleLabel: UILabel?
private var plusButton: UIButton?
private var editButton: UIButton?
var tap: UIGestureRecognizer?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isTranslucent = false
addTitleLabel()
addPlusButton()
addEditButton()
}
private func addTitleLabel() {
titleLabel = UILabel()
titleLabel?.textColor = Styling.navigationbarTintColor
titleLabel?.translatesAutoresizingMaskIntoConstraints = false
titleLabel?.font = UIFont(name: "DIN Alternate", size: 17)
if let titleLabel = titleLabel{
addSubview(titleLabel)
let leadingConstraint = NSLayoutConstraint(item: titleLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 15)
addConstraint(leadingConstraint)
let centerYConstraint = NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
}
}
private func addPlusButton(){
plusButton = UIButton(type: .custom)
plusButton?.setTitle("+", for: .normal)
plusButton?.layer.backgroundColor = Styling.navigationbarTintColor.cgColor
plusButton?.layer.cornerRadius = 15
plusButton?.setTitleColor(.black, for: .normal)
plusButton?.titleLabel?.font = UIFont(name: "DIN Alternate", size: 20)
plusButton?.contentVerticalAlignment = .center
plusButton?.contentHorizontalAlignment = .center
plusButton?.titleLabel?.baselineAdjustment = .alignCenters
if let plusButton = plusButton{
addSubview(plusButton)
plusButton.translatesAutoresizingMaskIntoConstraints = false
let trailingConstraint = NSLayoutConstraint(item: plusButton, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: -15)
addConstraint(trailingConstraint)
let centerYConstraint = NSLayoutConstraint(item: plusButton, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
let widthC = NSLayoutConstraint(item: plusButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(widthC)
let heightC = NSLayoutConstraint(item: plusButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(heightC)
}
}
private func addEditButton(){
editButton = UIButton(type: .custom)
editButton?.setTitle(LocalizedString("EDIT"), for: .normal)
editButton?.layer.backgroundColor = Styling.navigationbarTintColor.cgColor
editButton?.layer.cornerRadius = 15
editButton?.setTitleColor(.black, for: .normal)
editButton?.titleLabel?.font = UIFont(name: "DIN Alternate", size: 16)
editButton?.contentVerticalAlignment = .center
editButton?.contentHorizontalAlignment = .center
editButton?.titleLabel?.baselineAdjustment = .alignCenters
if let editButton = editButton{
addSubview(editButton)
editButton.translatesAutoresizingMaskIntoConstraints = false
let trailingConstraint = NSLayoutConstraint(item: editButton, attribute: .trailing, relatedBy: .equal, toItem: plusButton!, attribute: .leading, multiplier: 1.0, constant: -15)
addConstraint(trailingConstraint)
let centerYConstraint = NSLayoutConstraint(item: editButton, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
let widthC = NSLayoutConstraint(item: editButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 80)
addConstraint(widthC)
let heightC = NSLayoutConstraint(item: editButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(heightC)
}
}
func setTitle(title: String){
titleLabel?.text = title
}
}
There several ways to correct this:
Use + image, image will be centered correctly on UIButton.
Change title insets of the UIButton, give some more inset from bottom and left, UIButton has titleInsets property;
Change the font o_O which is silly idea but also work, you can edit the font and make "+" sign to be centered on the drawing area.
Hope this helps! Good luck!
func setupView()
{
self.blueView = UIView()
self.blueView?.backgroundColor = UIColor.blueColor()
self.blueView?.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.blueView!)
let blueViewCenterXConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 0.6, constant: 0)
let blueViewCenterYConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
let blueViewWidthConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.6, constant: 150)
let blueViewHeightConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150)
self.addConstraints([blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint])
self.redView = UIView()
self.redView?.backgroundColor = UIColor.redColor()
self.redView?.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.redView!)
let redViewCenterXConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.TrailingMargin , multiplier: 0.5, constant: 0)
let redViewCenterYConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Top, multiplier: 0.5, constant: 0)
let redViewWidthConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Width, multiplier: 0.5, constant: 150)
let redViewHeightConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Height, multiplier: 0.5, constant: 150)
self.addConstraints([redViewCenterXConstraint,redViewCenterYConstraint,redViewWidthConstraint,redViewHeightConstraint])
}
I have applied above code for Creating and Setting Auto Layout Constraint for UITextView for Universal App. I want to Put these both UITextView beside Each other for Every Device with the space of 10 between them horizontally and centered in Vertically. Will Anybody please be Grateful to fix mentioned issue, which would be helpful a lot to me.
I've taken the liberty of looking at you previous question and furthermore I've used NSLayoutAnchors (described here) because I think they are easier to read.
Based on the above, this UIView subclass:
import UIKit
class UIViewUsingTextField: UIView {
let width: CGFloat = 150.0
var blueview = UIView()
var redview = UIView()
init() {
super.init(frame: CGRect.zero)
self.backgroundColor = UIColor.whiteColor()
self.setupView()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setupView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
func setupView() {
//add blue view
blueview.backgroundColor = UIColor.blueColor()
blueview.translatesAutoresizingMaskIntoConstraints = false
addSubview(blueview)
//center the blue view and move it 1/2 view width to the left
blueview.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor, constant: -(width/2)).active = true
blueview.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true
blueview.widthAnchor.constraintEqualToConstant(width).active = true
blueview.heightAnchor.constraintEqualToConstant(width).active = true
//add red view
redview.backgroundColor = UIColor.redColor()
redview.translatesAutoresizingMaskIntoConstraints = false
addSubview(redview)
//place red view 10 px to the right of blue view
redview.leadingAnchor.constraintEqualToAnchor(blueview.trailingAnchor, constant: 10.0).active = true
redview.centerYAnchor.constraintEqualToAnchor(blueview.centerYAnchor).active = true
redview.widthAnchor.constraintEqualToAnchor(blueview.widthAnchor).active = true
redview.heightAnchor.constraintEqualToAnchor(blueview.heightAnchor).active = true
}
}
Gives me this layout
Hope this resembles what you were after.
I have included a solution using your code, but would also like to show you how it is done using constraintWithVisualFormat, because I think it reads easier.
Solution for you code:
Note that the width are set to a fixed value here. Not sure if this is really what you want.
func setupView()
{
self.blueView = UIView()
self.blueView?.backgroundColor = UIColor.blueColor()
self.blueView?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.blueView!)
let blueViewCenterXConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: -5)
let blueViewCenterYConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
let blueViewWidthConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 150)
let blueViewHeightConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150)
self.view.addConstraints([blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint])
self.redView = UIView()
self.redView?.backgroundColor = UIColor.redColor()
self.redView?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.redView!)
let redViewCenterXConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Trailing , multiplier: 1, constant: 10)
let redViewCenterYConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
let redViewWidthConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 1)
let redViewHeightConstraint = NSLayoutConstraint(item: self.redView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: blueView, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 1)
self.view.addConstraints([redViewCenterXConstraint,redViewCenterYConstraint,redViewWidthConstraint,redViewHeightConstraint])
}
Solution using visual constraints
let views = ["redView": redView!, "blueView": blueView!]
//setup the horizontal constraints to have 15 leading and trailing constraints, equal widths for blueView and redView and 10 spacing in between.
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[blueView(==redView)]-10-[redView]-15-|", options: .AlignAllCenterY, metrics: nil, views: views))
//set the height of the two views
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[blueView(150)]", options: .AlignAllCenterX, metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[redView(150)]", options: .AlignAllCenterX, metrics: nil, views: views))
//align the two views relative to the centre of the superview.
blueView!.centerYAnchor.constraintEqualToAnchor(view!.centerYAnchor, constant: 0).active = true
redView!.centerYAnchor.constraintEqualToAnchor(blueView!.centerYAnchor).active = true
Replace self.view with self for your example
I have the following code to simply center a red square using AutoLayout constraints programmatically in my ViewController's view:
class ViewController: UIViewController {
let square: UIView
required init?(coder aDecoder: NSCoder) {
let squareFrame = CGRectMake(0.0, 0.0, 500.0, 500.0)
self.square = UIView(frame: squareFrame)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
self.view.addSubview(self.square)
self.square.backgroundColor = UIColor.redColor()
self.view.backgroundColor = UIColor.blueColor()
print(self.square)
setupConstraints()
print(self.square)
}
func setupConstraints() {
self.square.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true
}
}
The resulting screen however only shows the blue background, no sign of the red square... Even when using the view debugging feature in Xcode it can't be seen.
If I comment out setupConstraints(), it works as "expected" with the original frame that I gave the square during initialisation.
By the way, both print statements have the exact same output:
<UIView: 0x7ff1c8d3f3e0; frame = (0 0; 500 500); layer = <CALayer: 0x7ff1c8d04c00>>
How can this be when the square is nowhere to be seen?
Update:
The issue remains when I am adding width and height constraints as suggested by #HaydenHolligan in setupConstraints():
func setupConstraints() {
self.square.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true
// the following lines have no effect with respect to the issue mentioned aboove
let sizeFormat = "[square(100#100)]"
let size = NSLayoutConstraint.constraintsWithVisualFormat(sizeFormat, options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["square": self.square])
self.view.addConstraints(size)
}
Try to change your setupConstraints func to this :
func setupConstraints() {
self.square.translatesAutoresizingMaskIntoConstraints = false
let centerX = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0)
let centerY = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0)
let squareWidth = NSLayoutConstraint(item: self.square, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:500)
let squareHeight = NSLayoutConstraint(item: self.square, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:500)
self.view.addConstraints([centerX , centerY ,squareWidth , squareHeight])
}
I have created my own custom UIView for a popover that I have to display on a screen. My UIView looks like this:
class HelpTipsPopover: UIView {
weak var title: UILabel!
weak var myText: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let strongTitle = UILabel()
title = strongTitle
let strongMyText = UILabel()
myText = strongMyText
self.addSubview(strongTitle)
title.translatesAutoresizingMaskIntoConstraints = false
if selected == true{
title.text = "Search"
title.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
title.textColor = UIColor.TRLMBlueBlackColor()
let leftConstraint = NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
let topConstraint = NSLayoutConstraint(item: title, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 10)
self.addConstraints([leftConstraint, topConstraint])
self.addSubview(strongMyText)
myText.translatesAutoresizingMaskIntoConstraints = false
myText.text = "Search equities to view the order book and market prints for specific moments in time."
myText.numberOfLines = 0
myText.lineBreakMode = NSLineBreakMode.ByWordWrapping
myText.font = UIFont(name: "Helvetica Neue", size: 12)
myText.textColor = UIColor.TRLMBlueBlackColor()
let leftDescription = NSLayoutConstraint(item: myText, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
let rightDescription = NSLayoutConstraint(item: myText, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 10)
let topDescription = NSLayoutConstraint(item: myText, attribute: .Top, relatedBy: .Equal, toItem: title, attribute: .Bottom, multiplier: 1.0, constant: 5)
self.addConstraints([leftDescription, topDescription, rightDescription])
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
}
Now I have three popovers that I have to display in my view controller with different title and text in each popover. Here are the methods in my View Controller which displays those popovers:
func showPopover(){
self.view.addSubview(helpTipsPopover)
helpTipsPopover.tag = 1
helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
helpTipsPopover.layer.cornerRadius = 6
helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
let leftConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 10)
let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotOne, attribute: .Bottom, multiplier: 1.0, constant: 4)
let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
self.view.addConstraints([leftConstraint, topConstraint, widthConstraint, heightConstraint])
}
func showPopoverTwo(){
self.view.addSubview(helpTipsPopover)
helpTipsPopover.tag = 1
helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
helpTipsPopover.layer.cornerRadius = 6
helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
let centerConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0)
let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotTwo, attribute: .Bottom, multiplier: 1.0, constant: 4)
let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
self.view.addConstraints([centerConstraint, topConstraint, widthConstraint, heightConstraint])
}
Now I want to achieve something like this:
So each popover should have different title and text but i want to reuse the same UIView. Any help is appreciated. Thanks!
Modify your init method
init(frame: CGRect, titleString: String, bodyString: String) {
super.init(frame: frame)
// Your current initializer
title.text = titleString
myText.text = bodyString
}
Then you can initialize a popup like this:
let frame = CGRectMake(0,0,180,100)
let titleString = "My Custom Title"
let bodyString = "This is a body. I'm explaining things to you here."
helpTipsPopoverOne = HelpTipsPopover(frame: frame, titleString: titleString, bodyString: bodyString)
Modify your display method to take a popup as a parameter. Then you can create popups and display with whatever text you'd like!