How to add border to a UIImage which has content mode set to aspect fill - uiimageview

I have set photoImageView.contentMode = UIViewContentMode.ScaleAspectFill.
I want to put a border for the image. But since the content mode is AspectFill, I don't know how to programmatically add a border just to the image. Is there any way to do this? I use Swift 2 and Xcode beta 4

Hi Jms i came up with few lines of code (I'm on beta 5,but i don't think there s difference)
#IBOutlet weak var image: UIImageView!
image.layer.borderColor = UIColor.blackColor().CGColor
image.layer.borderWidth = 4
image.clipsToBounds = true
Worked perfectly on my image, hope will help you.

Related

Problems with storyboard colors in mixed objective-c/swift project

I added a new view controller to my objective-c project. The new view controller is written in Swift. Everything compiles correctly and functions correctly except that no matter what color I choose for the view's background color, it either comes out white (default) or black. If I decrease the alpha for the default background color it just gets gray. If I have anything over about 0.4 alpha for any other color the view is black.
Second problem: the code in my Swift view controller to set background color for the view appears to have no effect. Relevant code is below. I first set the view background color to white with alpha 1.0 and then change it to the color I want. None of this has any effect. Is this because it has already been set in the storyboard? If so, how can I override the storyboard settings programmatically? I am very new to Swift but have been programming objective-c for about one year and I haven't seen this kind of problem in a pure objective-c project. Xcode version is 7.3.
I would really appreciate any help.
override func viewDidLoad() {
super.viewDidLoad()
self.username.delegate = self;
self.password.delegate = self;
self.navigationItem.setHidesBackButton(true, animated:true);
view.backgroundColor = UIColor(white: 0x255/255, alpha: 1.0)
//set the background color to blue-green
view.backgroundColor = UIColor(
red: 0x00/255,
green: 0x7d/255,
blue: 0x96/255,
alpha: 1.0)
It looks like these problems are due to auto layout problems. If there are any autolayout issues (even if they are not flagged by a red arrow in the project navigator), the storyboard produces a black screen. I'm not sure why this happens with Swift: objective-C would just produce a screen with some layout problems. Once all the autolayout issues get fixed (usually by using Reset to Suggested Constraints), there isn't a black screen. Also, my code for setting the screen background color then works.This was very frustrating and I don't know why it works this way when the code underlying the view is Swift and not objective-C. But if there is the tiniest variation in the storyboard scene that has a tiny autolayout problem, I get a black screen.

How to dynamically adjust the height of Today Extension

I have a today extension that displays both a table view and some text. I want to dynamically resize the widget so that it fits the height of the tableview.
I have tried using autolayout as well as:
self.preferredContentSize = self.tableview.contentSize
But the because my view is taller than just the tableview height it does not work.
I found a couple of example showing how to do it in Objective C but none in Swift.
Thank you for your help !

Swift Xcode aspect-fit behaviour using AutoLayout on a UIImageView inside a containerView

I have a embedded containerview inside my viewcontroller that leads to a UIPageController. Then the UIPageController loads images from a 3rd VC controller.
So it just a simple image swipe / image carousel.
What I cant get to work is autolayout on the UIImage. My containerview has a maring top 0 right 0 left 0 and a height of 200pt with aspect ratio set.
Bellow it some other information shows eg title / text etc.
But all images appears zoomed in/croped in the containerview.
So how can I make the images scale and fit inside the container view?
Thanks,
set the image view's contentmode to scale aspect fit to see as much of the image as possible.
imageView.contentMode = .ScaleAspectFit

Why is my art displayed at 2x on screen?

I am creating a Universal App.
I have created my art in illustrator to fit properly on an iPhone screen, specifically at 640x1136 px. I exported PNGs and named myArt#2x.png and also a 50% version named myArt.png. The art is in an atlas folder.
I am creating sprite nodes a like this:
NSString * backgroundName=#"starField";
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:backgroundName];
background.position = CGPointMake(screenWidth/2, screenHeight/2);
background.zPosition = LayerBackground;
[_worldNode addChild:background];
The problem is that when I run this in the simulator (iPhone 4 w/retina), the images are scaled up. I have to ousel xScale and yScale to bring it back to actual size.
Why? What am I doing wrong?
Thanks,
rich!
You need to set the size property of the SKNode I think. For example:
background.size = CGSizeMake(screenWidth, screenHeight);
Okay I think I solved it -
I don't start creating my content until viewWillLayoutSubviews had been called. The default SpritKit Template uses viewDidLoad, but I changed that to viewWillLayoutSubviews and everything looks fine now.
I think I was trying to create stuff before the view "sorted" itself out...
Not sure what the difference is but it worked.
-r

How do you create a semi-transparent background for a UIView?

I am creating a UIView containing some text that partially covers a UIImageView. I want the user to be able to read the text and still maintain a perspective on the image underneath. I have tried setting the background color to [UIColor clearColor], but then the background is totally transparent and it is hard to read the text depending upon the image colors.
If I lower the view.alpha=0.5 the whole view including the text is partially transparent. What I'd like is to maintain the text and reduce the transparency of the background partially, allowing the image to show through.
OPTION 1 - USING STORYBOARDS
For those who have their view in a storyboard or .xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.
If you need a background color that is partially transparent, select the desired background color with the color picker and use the Opacity slider at the bottom to set the transparency.
OPTION 2 - USING COLOR ASSETS (AND STORYBOARDS)
Another very useful option is to add colors to your .xcassets library, so that you can use the same color easily in different views. You can make these colors (semi-)transparent as well, here's how:
Open your .xcassets library
Add a Color Set
Give it a useful name and select the color thumbnail
In the Attributes Inspector you can then change the color and use the slider to adjust its opacity
Go back to your storyboard and select the view you need this transparent background color
In the Background option of in the Attributes Inspector you can now select the Color you added to your .xcassets library. This is very useful if you have multiple views across your app using the same background.
In code you can access the colors from your Color Assets using:
SWIFT (UIColor): UIColor(named: "DP Textfield")
SWIFTUI (Color): Color("DP Textfield")
I think what you mean is you want the backgroundColor of your UIView to be semi transparent? If you want white/transparent use this:
myView.backgroundColor = [UIColor colorWithWhite:myWhiteFloat alpha:myAlphaFloat];
else if you want it to be another color use the general UIColor method: +colorWithRed:green:blue:alpha:
This will work.
myView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
Eventually you already have a color so you could use .colorWithAlphaComponent like this:
let exampleColor = UIColor.blackColor()
overlayView.backgroundColor = exampleColor.colorWithAlphaComponent(0.8)
For Swift 4+ :
Black translucent view:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
I believe you should use:
myView.alpha = myAlphaFloat;
myView.opaque = NO;
For Xamarin C#, at this time, the visual storyboard does not have the "opacity" slider of Xcode's storyboard mentioned by Bocaxica.
If you set BackgroundColor for View nameOfView in storyboard, then in your view controller's ViewDidLoad, add this line to set alpha:
nameOfView.BackgroundColor = nameOfView.BackgroundColor.ColorWithAlpha( 0.7f ); // A value between 0 and 1.
Swift 3+
white half transparent:
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
or black half transparent:
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)

Resources