I am using adding Background in iOS Swift App using:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
However, it does not cover the Screen till bottom. I searched Other questions:
var backImage = UIImage(named: "background")
var resizablebackImage = backImage?.resizableImageWithCapInsets(UIEdgeInsets(top:10,left:0,bottom:10,right:0))
self.view.backgroundColor = UIColor(patternImage:resizablebackImage!)
That does not work. Any ideas?
If your intention is to set it as the background image, don't make image as pattern color and fill it as background color. Instead create an UIImageView set your image as it's image and add it to your UIView.
var bgImage = UIImage(named: "background");
var imageView = UIImageView(frame: self.view.bounds);
imageView.image = bgImage
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
For Swift 1.x & Swift 2 plus Device Rotation Detection
Please see my code below. Just set bgImageName to the name of your image and call setBackgroundImage(). I added an auto-reload in case of a device rotation.
var bgImage = UIImage()
var bgImageView = UIImageView()
var bgImageName = ""
override func viewDidLoad() {
super.viewDidLoad()
bgImageName = "bg-orange" // or whatever your image is called
setBackgroundImage()
}
func setBackgroundImage() {
if bgImageName > "" {
bgImageView.removeFromSuperview()
bgImage = UIImage(named: bgImageName)!
bgImageView = UIImageView(frame: self.view.bounds)
bgImageView.image = bgImage
self.view.addSubview(bgImageView)
self.view.sendSubviewToBack(bgImageView)
}
}
// detect device orientation changes
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("rotated device to landscape")
setBackgroundImage()
} else {
print("rotated device to portrait")
setBackgroundImage()
}
}
Related
I want to insert an image inside a ui navigation bar and make it look as an aspect fit image looks inside an imageView.
i am doing it as
navigationController?.navigationBar.compactAppearance?.backgroundImageContentMode = .scaleToFill
navigationController?.navigationBar.setBackgroundImage(image, for: .default)
But it is giving me the output as :
How can i make it perfect.
The simplest way to use the UIImageView here with scaleAspectFit image property. Try the following working code block.
func setNavigation() {
let imageView = UIImageView(image: UIImage(named: "bankselected"))
imageView.frame = navigationController!.navigationBar.frame
imageView.backgroundColor = .lightGray
imageView.contentMode = .scaleAspectFit
navigationController?.navigationBar.addSubview(imageView)
}
You can use this Protocol to add image in your navigationBar
protocol CustomizableNavigationBar {
var navigationBar: UINavigationBar? { get }
}
extension CustomizableNavigationBar {
func setNavBarLogo() {
let imageView = UIImageView(image: UIImage(named: "artistAvatar1"))
imageView.frame = navigationBar!.frame
imageView.contentMode = .scaleToFill
navigationBar?.addSubview(imageView)
}
}
Use it like This
class SearchViewController: UIViewController,CustomizableNavigationBar {
var navigationBar: UINavigationBar?
override func viewDidLoad() {
super.viewDidLoad()
navigationBar = navigationController?.navigationBar
setNavBarLogo()
}
}
The title itself explains as much as i will be able to. Basically, i have a segmented control that i want to change the image within my image view after each segment is selected, but i can't seem to apply or find the logic to resolve my problem. The answer itself is probably very simple, but i'm really new to Swift and didn't manage to find an answer anywhere, so i would love a solution to my problem. Thanks!
#IBOutlet weak var newImageView: UIImageView!
#IBAction func chooseImage(sender: AnyObject) {
if myPhotoSegment.selectedSegmentIndex == 0
{
newImageView = UIImage(named: "3.jpg")
}
if myPhotoSegment.selectedSegmentIndex == 1
{
}
You can't set the value of a UIImageView to a UIImage.
Set the value of newImageView.image instead. This is also a good opportunity for a switch statement.
#IBAction func chooseImage(sender: AnyObject) {
switch myPhotoSegment.selectedSegmentIndex {
case 0:
newImageView.image = UIImage(named: "3.jpg")
case 1:
newImageView.image = UIImage(named: "4.jpg")
default:
newImageView.image = nil
}
}
Segment using image in imageview in swift4
#IBOutlet weak var bgimage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red
let itemsimg = ["google", "facebook", "instagram" , "twitter"]
let customSI = UISegmentedControl(items: itemsimg)
customSI.selectedSegmentIndex = 0
customSI.frame = CGRect(x:50, y:100, width:300, height: 40)
customSI.layer.cornerRadius = 5.0
customSI.backgroundColor = UIColor.black
customSI.tintColor = UIColor.white
customSI.addTarget(self, action: #selector(changeimg),for: .valueChanged)
self.view.addSubview(customSI)
}
#objc func changeimg(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 1:
bgimage.image = UIImage(named: "facebook")
case 2:
bgimage.image = UIImage(named: "instagram")
case 3:
bgimage.image = UIImage(named: "twitter")
default:
bgimage.image = UIImage(named: "google")
}
}
}
I'm trying to change the indicator image , following the example in EAIntroView
here is the objective-c code from EAIntroView
SMPageControl *pageControl = [[SMPageControl alloc] init];
pageControl.pageIndicatorImage = [UIImage imageNamed:#"pageDot"];
pageControl.currentPageIndicatorImage = [UIImage imageNamed:#"selectedPageDot"];
[pageControl sizeToFit];
intro.pageControl = (UIPageControl *)pageControl;
intro.pageControlY = 130.f;
and here is the swift code I'm trying to implement
// SMPageControl
pageControl = SMPageControl()
pageControl.pageIndicatorImage = UIImage(named: "pageDot")
pageControl.currentPageIndicatorImage = UIImage(named: "selectedPageDot")
pageControl.sizeToFit()
pageControl.backgroundColor = UIColor.clearColor()
intro.pageControl = pageControl as? UIPageControl
swift code has a warning here
intro.pageControl = pageControl as? UIPageControl
the warning is :
Cast from 'SMPageControl!' to unrelated type 'UIPageControl' always fails
any help ?
For changing page Indicator Image [SMPageControl] / EAIntroView in swift I have created custom class of UIPageControl like below:
import UIKit
class CustomPageControl: UIPageControl {
let activePage: UIImage = UIImage(named: "icon-selected-dot")!
let inActivePage: UIImage = UIImage(named: "icon-dot")!
override var numberOfPages: Int {
didSet {
updateDots()
}
}
override var currentPage: Int {
didSet {
updateDots()
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
}
func updateDots() {
var i = 0
for view in self.subviews {
var imageView = self.imageView(forSubview: view)
if imageView == nil {
if i == self.currentPage {
imageView = UIImageView(image: activePage)
} else {
imageView = UIImageView(image: inActivePage)
}
imageView!.center = view.center
view.addSubview(imageView!)
view.clipsToBounds = false
}
if i == self.currentPage {
imageView!.alpha = 1.0
imageView?.image = activePage
} else {
imageView!.alpha = 0.5
imageView?.image = inActivePage
}
i += 1
}
}
fileprivate func imageView(forSubview view: UIView) -> UIImageView? {
var dot: UIImageView?
if let dotImageView = view as? UIImageView {
dot = dotImageView
} else {
for foundView in view.subviews {
if let imageView = foundView as? UIImageView {
dot = imageView
break
}
}
}
return dot
}
}
then in your class just add these lines and you can use custom images for dot
//Custom page Control
let pageControl = CustomPageControl()
pageControl.updateDots()
intro.pageControl = pageControl
Hope it will work for you! #ynamao
You can see from the source code of SMPageControl that it isn't a subclass of UIPageControl. Which means the error is expected: UIPageControl is a completely unrelated type, to which the value cannot be cast.
The Objective-C you pointed to might work, but it's bad and wrong: inline cast to UIPageControl achieves nothing here and can cause internal inconsistencies.
This is exactly the kind of sloppiness that Swift compiler is designed to prevent, and it's doing its job well.
Your best bet is to forgo using this library in Swift code.
I've created UITabBarController in my application.
Then in viewDidLoad() I want to change UITabBar background image. Here is the code I'm trying to make it works:
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().translucent = false
UITabBar.appearance().backgroundColor = UIColor.clearColor()
UITabBar.appearance().backgroundImage = UIImage(named: "tabbar_background")
UITabBar.appearance().contentMode = .ScaleAspectFit
}
}
But the result isn't correct (image). Can someone help me to make it fill the entire tabbar frame?
I solved this problem using clipsToBounds.
Here are my example:
let tabBar = self.tabBar
tabBar.backgroundImage = UIImage()
tabBar.clipsToBounds = true
This work perfectly on iPhone X
Try resizing the image to the tab bar size. Or Add an imageView to the tabBar as subview, and then use the image in that imageView.
Subclass the TabBarController and add imageview there:
var bgView: UIImageView = UIImageView(image: UIImage(named: "tabBarBackground.png"))
bgView.frame = CGRectMake(0, 420, 320, 60)//you might need to modify this frame to your tabbar frame
self.view.addSubview(bgView)
This works for me in swift 3
class MyTabBarController: UITabBarController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImageView(image: UIImage(named: "gray background"))
backgroundImage.frame = backgroundImage.bounds
self.view.addSubview(backgroundImage)
}
Here is code for a custom translucent tab bar image with custom icons. You will need to refer directly to the tab bar via a variable.
Swift 3
private func setupTabBar() {
print ("Setting up the Tab Bar and Items")
tabBarView.clipsToBounds = true
tabBarView.backgroundImage = UIImage()
let bgView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "TabBarBackground"))
bgView.frame = tabBarView.bounds
tabBarView.addSubview(bgView)
tabBarView.tintColor = UIColor.white
UITabBar.appearance().tintColor = UIColor.gray
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.gray], for: .selected)
tabBarLimitsItemView.image = #imageLiteral(resourceName: "TabIconLimits").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBarControlsItemView.image = #imageLiteral(resourceName: "TabIconControls").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBarPayRulesItemView.image = #imageLiteral(resourceName: "TabIconPayRules").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBarSettingsItemView.image = #imageLiteral(resourceName: "TabIconSettings").withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
I'm using this custom collectionView transition:
When a cell is selected, the UIImage in the cell is animated to transition seamlessly into the UIImageView in the detailViewController ,as seen in the GIF in this link.
However the issue is, the UIImage in the detailViewController's UIImageView is hard coded in, and will be the same for every selected cell from the UICollectionView. I have different images within my collectionViewCell's and this would obviously be a problem, so I need to somehow programmatically make my detailViewController's UIImageView Image to equal that of the selected collectionViewCell's UIImageView Image when the transition is completed.
Here's the code I used to try to achieve this:
class DetailViewController: UIViewController, ARNImageTransitionZoomable {
#IBOutlet weak var imageView: UIImageView!
var selectedCell = MasterCollectionViewController()
deinit {
println("deinit DetailViewController")
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.layer.cornerRadius = 3.0
imageView.clipsToBounds = true
}
// MARK: - ARNImageTransitionZoomable
func createTransitionImageView() -> UIImageView {
var imageView = UIImageView(image: self.imageView.image)
imageView.contentMode = self.imageView.contentMode
imageView.clipsToBounds = true
imageView.userInteractionEnabled = false
imageView.frame = self.imageView!.frame
return imageView
}
func presentationBeforeAction() {
self.imageView.hidden = true
}
func presentationCompletionAction(completeTransition: Bool) {
self.imageView.hidden = false
self.imageView.image = selectedCell.selectedImageView?.image
}
func dismissalBeforeAction() {
self.imageView.hidden = true
}
func dismissalCompletionAction(completeTransition: Bool) {
if !completeTransition {
self.imageView.hidden = false
}
}
}
I added a variable "selectedCell" that is of type MasterCollectionViewController(), giving me reference to everything in that class.
Then in the animation/transitions completion, I attempted to set the selected cell's UIImage to that of my detailViewController's UIImageView Image with the line
func presentationCompletionAction(completeTransition: Bool) {
self.imageView.hidden = false
self.imageView.image = selectedCell.selectedImageView?.image
}
This didn't work, when the transition is complete, the UIImageView is just white with no image, just as it is on my storyboard. Somebody know any method I can implement?