iOS navigationItem titleView image and text - ios

I want to set the navigationItem's titleView with image and text like the picture below:
How do I achieve that?
More specifically, I am trying to do this in Swift.

The condition, your controller must have its navigation controller:
Using my code I write below, the result is like this:
In your view controller ,try my code below:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initUI()
}
// MARK: - init
func initUI() {
let rect:CGRect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 64, height: 64))
let titleView:UIView = UIView.init(frame: rect)
/* image */
let image:UIImage = UIImage.init(named: "01.jpg")!
let image_view:UIImageView = UIImageView.init(image: image)
image_view.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
image_view.center = CGPoint.init(x: titleView.center.x, y: titleView.center.y - 10)
image_view.layer.cornerRadius = image_view.bounds.size.width / 2.0
image_view.layer.masksToBounds = true
titleView.addSubview(image_view)
/* label */
let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: 64, height: 24))
label.text = "Hello"
label.font = UIFont.systemFont(ofSize: 12)
label.textAlignment = .center
titleView.addSubview(label)
self.navigationItem.titleView = titleView
}

Related

Swift Add UIView with subviews to titleView

I'd like to customize my navigation bar and insert a UIView with two subviews into the title view of the navigation bar.
Unfortunately the subviews aren't displayed.
Do you have any idea why?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
problem and expected solution:
Why did you use height of all the element as UIScreen.main.bounds.size.height? Please try the below code.
P.S. I have changed the color of searchIcon as i didn't have the image and changed the color of text of label and added some text so that it is visible.
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
Here is the screenshot of the above code:
Please change the x,y accordingly as per your requirement.
Have you considered making your let bar = UINavigationBar() programmatically and then add it with: self.view.addSubview(bar)?
So you can push all the "UIBarItems" you want..?

how to set the position of the placeholder of a placeholder in swift programatically ?

I am trying to create the following registration form . I cant set the space between the textfield image and the placeholder text . I've tried the following :
Email_text.placeholderRect(forBounds: CGRect(x: 10, y: 0 , width: 60, height: 50))
any idea how can I fix this ?
Thanks
You can simply do like this, an extension for UITextField
extension UITextField {
func setTextIconAndPlaceholder(icon: UIImage, placeholder: String) {
let imageView = UIImageView()
imageView.image = icon
imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let view = UIView()
view.frame = CGRect(x: 0, y: 2, width: 25, height: 25)
view.backgroundColor = UIColor.clear
view.addSubview(imageView)
self.leftView = view
self.leftViewMode = UITextFieldViewMode.always
self.placeholder = placeholder
}
}

Custom titleView changes location after push and pop new screen in stack

I'm trying to implement custom titleView for navigation controller with "title" and "description" labels. If I placed this titleView on first VC, it looks good. If I push second VC into navigation stack and pop it, location of the titleView is changed.
titleView's constraints:
titleLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
descriptionLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 2.0).isActive = true
In viewDidLoad of VC I use follow code to insert titleView:
navigationItem.titleView = BarTitleView()
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
I tried to insert follow lines into viewWillAppear (second VC has different bar buttons and it can be root of problem), but nothing changes
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
How can I fixed this issue?
I have created a Title and Subtitle in the Navigation bar without the need for Interface Builder and constraint modification.
My Function to create the title view looks as follows:
class func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x: 0, y: -8, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 12, width: 0, height: 0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.white
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
// Fix incorrect width bug
if (subtitleLabel.frame.size.width > titleLabel.frame.size.width) {
var titleFrame = titleLabel.frame
titleFrame.size.width = subtitleLabel.frame.size.width
titleLabel.frame = titleFrame
titleLabel.textAlignment = .center
}
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: titleLabel.frame.size.height))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff < 0 {
let newX = widthDiff / 2
subtitleLabel.frame.origin.x = abs(newX)
} else {
let newX = widthDiff / 2
titleLabel.frame.origin.x = newX
}
return titleView
}
Then to use the title view in any view controller I just call this line:
self.navigationItem.titleView = Functions.Views.setTitle(title: "Title String", subtitle: "Subtitle String")

IOS, Swift: creating a split screen with 2 views, with animation

I'm an absolute IOS newbie studying Swift.
I'm trying to create an app with a calendar UI control on top (already got that working), and a listview on the bottom.
Exactly like the picture on the bottom (and I apologise for the huge pic).
Now, like the calendar app, the screen will only show the calendar control, but if you click a button - the listview will reveal itself on the bottom and the screen will become split.
The only way I can think of doing this is to generate the UITableView in code, assigning the current ViewController as both delegate and datasource, and animating both the views (calendar and listview) to grow/shrink to split the screen.
I don't want to use StackView because that's IOS-9 and up.
Is there a better way to do this?
I suspect you're trying to do something like this. Hope it helps:
Swift 2:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.Center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRectMake(0,0,200,100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", forState: .Normal)
btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
view1.addSubview(btn)
view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.Center
view2.addSubview(lbl2)
}
var c = 0
func showView2(sender : UIButton) {
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", forState: .Normal)
}
else {
self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", forState: .Normal)
}
}, completion: { finished in
})
c++
}
Swift 3:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", for: .normal)
btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
view1.addSubview(btn)
view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.center
view2.addSubview(lbl2)
}
var c = 0
func showView2(_ sender : UIButton) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", for: .normal)
}
else {
self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", for: .normal)
}
}, completion: { finished in
})
c += 1
}

IOS: Add activity indicatior view in web view

class ViewController: UIViewController , UIWebViewDelegate {
var webview = UIWebView()
var boxView = UIView()
#IBAction func webview(sender: UIButton) {
let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activityView.frame = CGRect(x: 20, y: 12, width: 40, height: 40)
activityView.startAnimating()
webview.addSubview(activityView)
webview.reload()
let url = NSURL(string:"http://www.sugoilabs.com")
UIApplication.sharedApplication().openURL(url!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func webViewDidStartLoad(webView_Pages: UIWebView) {
webview.delegate = self
// Box config:
boxView = UIView(frame: CGRect(x: 115, y: 110, width: 80, height: 80))
boxView.backgroundColor = UIColor.blackColor()
boxView.alpha = 1
boxView.layer.cornerRadius = 10
// Spin config:
let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activityView.frame = CGRect(x: 20, y: 12, width: 40, height: 40)
activityView.startAnimating()
// Text config:
let textLabel = UILabel(frame: CGRect(x: 0, y: 50, width: 80, height: 30))
textLabel.textColor = UIColor.blackColor()
textLabel.textAlignment = .Center
textLabel.font = UIFont(name: textLabel.font.fontName, size: 13)
textLabel.text = "Loading..."
// Activate:
boxView.addSubview(activityView)
boxView.addSubview(textLabel)
self.view.addSubview(boxView)
}
func webViewDidFinishLoad(webView_Pages: UIWebView) {
// Removes it:
boxView.removeFromSuperview()
}
}
I'm guessing your mySpinner ivar isn't being properly retained or declared.
In your .h file, declare it as a property. That is:
#property (strong) UIActivityIndicatorView *mySpinner;
then, when you create it:
self.mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
and when you reference it in your button click method:
[self.mySpinner startAnimating];

Resources