ImageView is a component we often use, but we find that there is no design for picture borders in the property editor of storyboard. Add a normal picture to the view, the effect is this:
Can see, the white background of the dark picture, the effect is always a little abrupt, we want to add a shadow border to transition a piece and background, open the picture belongs to the controller code, the class is displayed in the picture named image, now to set its border. Enter the following code in Viewdidload:
Image.layer.backgroundColor = Uicolor.orangecolor (). Cgcolor Image.layer.shadowColor = Uicolor.blackcolor (). Cgcolor image.layer.shadowOffset = cgsizemake (0, 0) image.layer.shadowOpacity = 0.5 Image.layer.shadowRadius = 10.0
Using the layer attribute, command to look at the source of the layer:
var layer:calayer {Get}//Returns view ' s layer. Would always return a non-nil value. View is layer ' s delegate
Can see it is a calyer type, Calyer type has a lot of methods, interested small partners can click to see.
Note that the property of layer backgroundcolor is not the type of uicolor that we commonly use, it is a cgcolor type, and if you use a strong-turn statement it won't work:
Image.layer.backgroundColor = Uicolor.orangecolor () as Cgcolor
or a
Image.layer.backgroundColor = (Cgcolor) uicolor.orangecolor ()
is not feasible, the possible way is the above code, in the following add one. Cgcolor.
Now run for a look at the effect:
Now the border shadow has been added, is not very flashy?
Swift UI special training Imagevi picture Border Shadow