I tried to use this code under in my app delegate in order to add a PNG image as a view controller's background :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
return YES;
}
but I have no luck with it... the view controller still has a white background. What's wrong with that code?
The accepted answer and Michael's answer will work, however, proper way is to use a UIImageView instead. It gives more control over resizing, scaling etc according to different screen sizes on devices. Here is the example;
First create a UIImage.
UIImage *backgroundImage = [UIImage imageNamed:#"iphone_skyline3.jpg"];
Second create a UIImageView. Set the frame size to the parent's (self) frame size. This is important as the frame size will vary on different devices. Stretching will occur depending on the image size. Next assign the image to the view.
UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
backgroundImageView.image=backgroundImage;
Finally, to keep the image behind all controls do the following. It is important if you are setting the image as a background for your app.
[self.view insertSubview:backgroundImageView atIndex:0];
You need to set the background for your ViewController's view
In your ViewController init or viewDidLoad:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
Here is how it is in swift:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}
Swift 4 version of the #hadaytullah answer with some improvements in image adjustments:
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImage.init(named: "yourImageNameHere")
let backgroundImageView = UIImageView.init(frame: self.view.frame)
backgroundImageView.image = backgroundImage
backgroundImageView.contentMode = .scaleAspectFill
backgroundImageView.alpha = 0.1
self.view.insertSubview(backgroundImageView, at: 0)
}
You could also write extension to UIViewController to use in multiple places
extension UIViewController {
func setBackgroundImage(imageName: String) {
let backgroundImage = UIImage(named: imageName)
let backgroundImageView = UIImageView(frame: self.view.frame)
backgroundImageView.image = backgroundImage
self.view.insertSubview(backgroundImageView, at: 0)
}
}
In viewDidLoad, I use:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImageString.png"]];
Alternatively, if you want to do it in appDelegate, I think its possible to
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImageString.png"]]];
Then in viewDidLoad set background to [UIColor clearColor] ?
If your view controller is having tableView/collection view the below code will suitable for you.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
}
you may consider the following solution i use in swift 3.0 and tested in xcode 8
// init and image with your pattern image
var bgUIImage : UIImage = UIImage(named: "yourIamgeName")!
let myInsets : UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
bgUIImage = bgUIImage.resizableImage(withCapInsets: myInsets)
self.view.backgroundColor = UIColor.init(patternImage:bgUIImage)
the result will be the following image
You can do it from the storyboard.
Add UIImageView and just set your background image for the Image property in the Attribute Inspector.
Related
I am struggling to enable zooming in a UIPopoverController using a UIScrollView. The scrollView is 600x600 and it should display a view controller that displays a UIImageView. The image appears except it's not centered.
This is the code from the viewDidLoad method in the view controller that is displayed in the popover.
- (void)viewDidLoad {
self.imageView = [[UIImageView alloc] init];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = self.image;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor greenColor];
[self.scrollView setContentSize:self.imageView.image.size];
[self.scrollView setDelegate:self];
[self.imageView setFrame:CGRectMake(0, 0, 600, 600)];
self.view = self.scrollView;
[self.scrollView addSubview:self.imageView];
// Do any additional setup after loading the view.
}
The UIScrollView and UIImageView are declared as properties inside the UIViewController that's displayed in the popover. image is another property that is set to point to an image when the UIViewController is created.
This is what is looks like.
I would like to center the image in the popover and make it fit. How can I do that? Thanks.
what you need to do is:
first:
in your uiviewcontroller which is presented as a popover
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
preferredContentSize = CGSize(width: collectionPreference.bounds.width, height: collectionPreference.bounds.height)
println("I am appearing?")
}
then from the viewcontroller that is presenting the popover:
make it conform to UIPresentationStyleForPresentingController protocol,
and implement this two things:
number one:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
number two: in your prepareforsegue method:
if let identifier = segue.identifier {
if identifier == "segueToYourPopoverVC" {
if let pVC = segue.destinationViewController as? YourPopoverVC {
let ppc = pVC.popoverPresentationController
ppc?.delegate = self
}
}
}
best of luck
What I've heard there are no real way) to set a background image for a view in swift (tex self.view.background image. After googling around I found this:
var imageView = UIImageView(frame: self.view.frame) // set as you want
var image = UIImage(named: "fade bg3")!
imageView.image = image
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
It worked but the statusbar (with the clock, battery status and network ex) has that background too. Can I prevent this?
This was the solution
UIImageView(frame: CGRectMake(self.view.frame.origin.x, 20, self.view.frame.size.width, self.view.frame.size.height))
Just Hide your Status bar for those screens.
You really should implement prefersStatusBarHidden on your view controller(s):
override func prefersStatusBarHidden() -> Bool {
return true
}
I have a UIImageView that should animate an array of images called capturedImages. I have the animation set up like so:
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 48, 320, 520);
imageView.animationImages = capturedImages;
imageView.animationDuration = 3.0;
imageView.animationRepeatCount = 0; //animate forever
[imageView startAnimating];
[self.view addSubview:imageView];
The images in capturedImages are taken with the phone/camera view upright. However, when the imageView displays them, they animate properly but are rotated 90 degrees counterclockwise. Is there a way to change the orientation of animationImages, or do I have to set each image's orientation individually?
The simplest solution may be to just rotate the UIImageView itself.
You can use the transform property of UIView to do this:
[imageView setTransform:CGAffineTransformMakeRotation(M_PI_2)];
Be aware of the fact that this will break if you start displaying images which are already in the correct orientation. These will be rotated 90 degrees clockwise as well.
Setting the image property for the UIImageView to the first image in the list of images fixes the problem. For example in my view controller (swift):
override func viewDidLoad() {
super.viewDidLoad()
if let images = self.images {
if images.count > 0 {
self.animation_view.image = images[0]
}
self.animation_view.animationImages = images
self.animation_view.animationRepeatCount = 0
self.animation_view.animationDuration = 1.0
}
}
override func viewWillAppear(animated: Bool) {
self.animation_view.startAnimating()
}
Is it possible to fix the position of a UITableViewController background image?
I've added the background image via the viewdidload method with the following code -
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blurredBGW"]];
But as the table is fairly long (and also contains static cells if that matters) it scrolls (and repeats) as you scroll down the table.
you can try to add the background image as UIView and not as UIColor, try this:
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"blurredBGW.png"]];
for swift version 3.0.try this code.it will set a fixed background to a table view controller
self.tableView.backgroundView = UIImageView(image: UIImage(named: "background.png")!)
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
Make the background of your table (and the cells) transparent, and set the image for the view underneath the table.
for
swift 2.2
you may use the following line of code
self.tableView.backgroundView = UIImageView(image: UIImage(named: "xxxx.png"))
I prefer to configure background with creating its specific xib, because background can be difficult. I do next:
1) Create a class, that helps me to load nib
class UIXIBView: UIView {
var view: UIView!
func setupXIBWithName(_ nibName: String) {
view = self.getViewFromNibWithName(nibName)
self.addSubview(view)
view.frame = self.bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
}
func getViewFromNibWithName(_ nibName: String) -> UIView {
let bundle = Bundle.main
let views = bundle.loadNibNamed(nibName, owner: self, options: nil)! as [AnyObject]
let view = views[0] as! UIView
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupXIBWithName(String(describing: type(of: self)))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupXIBWithName(String(describing: type(of: self)))
}
}
2) I create your specific view, for example let it be LKBackgroundView.swift, and inherit it from UIXIBView
class LKBackgroundView: UIXIBView {
// You can add your own logic there or override smth
}
3) I create xib with the same name LKBackgroundView.xib
4) I set FileOwner's class to LKBackgroundView inside xib, don't make a mistake by setting LKBackgroundView to the view class
5) Configure you xib like you want
6) Initialize your view inside ViewDidLoad inside your View Controller
override func viewDidLoad() {
super.viewDidLoad()
LKBackgroundView(frame:tableView.bounds)
tableView.backgroundView = backgroundView
}
7) Test it
Xamarin/C# Solution
TableView.BackgroundView = new UIImageView(UIImage.FromBundle("blurredBGW"))
I'm trying to customize UISearchBar but I"m having a hard time with it.
This is what my searchbar looks like currently. What I want to be able to do is set a uiimage on the entire uisearchbar. I tried setSearchFieldBackgroundImage but it does not affect the text field part of the searchbar. How can I put an image in the white part? I also want to extend the white part to the edge to have equal margins on all sides.
for the image purpose u could use this one,dont forget to import Quartzcore framework
[searchbar setBackgroundImage:[UIImage imageNamed:#"base_200x50.png"]];
[searchbar setTranslucent:YES];
UITextField *txtSearchField = [searchbar valueForKey:#"_searchField"];
[txtSearchField setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"base_200x50.png"]]];
[txtSearchField setBorderStyle:UITextBorderStyleRoundedRect];
txtSearchField.layer.borderWidth = 8.0f;
txtSearchField.layer.cornerRadius = 10.0f;
txtSearchField.layer.borderColor = [UIColor clearColor].CGColor;
and to extend textfeild u can set frame of txtSearchField.
I hope this may help you:
Pattern image I used:
#IBOutlet weak var sbSearchBar: UISearchBar!
// if you wan to set background color for searchbar, except field
func setupSearchbarBackgroundImage() -> Void {
sbSearchBar.setBackgroundImage(UIImage(named: "pattern2"), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
}
Result:
func setupSearchbar() -> Void {
// if you wan to set background color for field only
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.backgroundColor = UIColor(patternImage: UIImage(named: "pattern2")!)
}
}
Here is result: