Customize Navigation Bar Swift
up vote
1
down vote
favorite
I have a Navigation bar in my app and am trying to add a right button that is flush to the right side of the page. However whenever I try to anchor it to the right it is still the 16px margin from the side.
My code is as follows:
func setupNavigationBarItems(){
//title label
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
//Set logo Colors
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSAttributedStringKey.font:UIFont(name: Helpers.Fonts.Medium, size: 24.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.White, range: NSRange(location:0,length:12))
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.Red, range: NSRange(location:12,length:1))
titleLabel.attributedText = myMutableString
//freeBusy button
let freeBusyButton = UIButton(type: .system)
freeBusyButton.setImage(#imageLiteral(resourceName: "free").withRenderingMode(.alwaysOriginal), for: .normal)
freeBusyButton.frame = CGRect(x: 0, y: 0, width: view.frame.width + 32, height: view.frame.height)
freeBusyButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
freeBusyButton.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
freeBusyButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationItem.titleView = titleLabel
navigationController?.navigationBar.isTranslucent = false
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:freeBusyButton)]
}
ios swift uinavigationbar
add a comment |
up vote
1
down vote
favorite
I have a Navigation bar in my app and am trying to add a right button that is flush to the right side of the page. However whenever I try to anchor it to the right it is still the 16px margin from the side.
My code is as follows:
func setupNavigationBarItems(){
//title label
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
//Set logo Colors
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSAttributedStringKey.font:UIFont(name: Helpers.Fonts.Medium, size: 24.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.White, range: NSRange(location:0,length:12))
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.Red, range: NSRange(location:12,length:1))
titleLabel.attributedText = myMutableString
//freeBusy button
let freeBusyButton = UIButton(type: .system)
freeBusyButton.setImage(#imageLiteral(resourceName: "free").withRenderingMode(.alwaysOriginal), for: .normal)
freeBusyButton.frame = CGRect(x: 0, y: 0, width: view.frame.width + 32, height: view.frame.height)
freeBusyButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
freeBusyButton.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
freeBusyButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationItem.titleView = titleLabel
navigationController?.navigationBar.isTranslucent = false
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:freeBusyButton)]
}
ios swift uinavigationbar
1
You can follow this answer
– Kamran
Nov 11 at 4:20
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a Navigation bar in my app and am trying to add a right button that is flush to the right side of the page. However whenever I try to anchor it to the right it is still the 16px margin from the side.
My code is as follows:
func setupNavigationBarItems(){
//title label
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
//Set logo Colors
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSAttributedStringKey.font:UIFont(name: Helpers.Fonts.Medium, size: 24.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.White, range: NSRange(location:0,length:12))
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.Red, range: NSRange(location:12,length:1))
titleLabel.attributedText = myMutableString
//freeBusy button
let freeBusyButton = UIButton(type: .system)
freeBusyButton.setImage(#imageLiteral(resourceName: "free").withRenderingMode(.alwaysOriginal), for: .normal)
freeBusyButton.frame = CGRect(x: 0, y: 0, width: view.frame.width + 32, height: view.frame.height)
freeBusyButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
freeBusyButton.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
freeBusyButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationItem.titleView = titleLabel
navigationController?.navigationBar.isTranslucent = false
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:freeBusyButton)]
}
ios swift uinavigationbar
I have a Navigation bar in my app and am trying to add a right button that is flush to the right side of the page. However whenever I try to anchor it to the right it is still the 16px margin from the side.
My code is as follows:
func setupNavigationBarItems(){
//title label
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
//Set logo Colors
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSAttributedStringKey.font:UIFont(name: Helpers.Fonts.Medium, size: 24.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.White, range: NSRange(location:0,length:12))
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.Red, range: NSRange(location:12,length:1))
titleLabel.attributedText = myMutableString
//freeBusy button
let freeBusyButton = UIButton(type: .system)
freeBusyButton.setImage(#imageLiteral(resourceName: "free").withRenderingMode(.alwaysOriginal), for: .normal)
freeBusyButton.frame = CGRect(x: 0, y: 0, width: view.frame.width + 32, height: view.frame.height)
freeBusyButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
freeBusyButton.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
freeBusyButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationItem.titleView = titleLabel
navigationController?.navigationBar.isTranslucent = false
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:freeBusyButton)]
}
ios swift uinavigationbar
ios swift uinavigationbar
asked Nov 11 at 4:00
gtgaito
298
298
1
You can follow this answer
– Kamran
Nov 11 at 4:20
add a comment |
1
You can follow this answer
– Kamran
Nov 11 at 4:20
1
1
You can follow this answer
– Kamran
Nov 11 at 4:20
You can follow this answer
– Kamran
Nov 11 at 4:20
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245732%2fcustomize-navigation-bar-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can follow this answer
– Kamran
Nov 11 at 4:20