Ios8: Auto-Layout and Gradient

iOS8: Auto-layout and Gradient

Simply do it this inside viewDidLayoutSubviews:

override func viewDidLayoutSubview() {
super.viewDidLayoutSubviews
backgroundColor.frame = self.graphView.bounds
}

viewDidLayoutSubviews should be called when you rotate the device.

If it is not called, override this method and do it as,

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

backgroundColor.frame = self.graphView.bounds
}

Applying gradient background for UIView using auto layout

You can subclass the UIView and override drawRect method where you add your gradient.

Updated to Swift 4


class GradientView: UIView {

private let gradient : CAGradientLayer = CAGradientLayer()
private let gradientStartColor: UIColor
private let gradientEndColor: UIColor

init(gradientStartColor: UIColor, gradientEndColor: UIColor) {
self.gradientStartColor = gradientStartColor
self.gradientEndColor = gradientEndColor
super.init(frame: .zero)
}

required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
gradient.frame = self.bounds
}

override public func draw(_ rect: CGRect) {
gradient.frame = self.bounds
gradient.colors = [gradientEndColor.cgColor, gradientStartColor.cgColor]
gradient.startPoint = CGPoint(x: 1, y: 0)
gradient.endPoint = CGPoint(x: 0.2, y: 1)
if gradient.superlayer == nil {
layer.insertSublayer(gradient, at: 0)
}
}
}

After you create your UIView you just need to add your constraints to that view.

iOS8: viewDidLayoutSubviews() hides elements in Storyboard Auto-Layout

Have you tried bringing the label to the front?

graphView.bringSubviewToFront(label)

UIButton gradient effect issue on viewDidLoad

Add the code in viewDidLayoutSubviews. To call it once, declare a boolean that set to true when the view did finish laying out subview.

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if !viewDidLayoutSubviews {
viewDidLayoutSubviews = true
// Code here
}
}

Auto Layout a background image to work on all devices

First off, you need to set the top, leading (left), trailing (right) and bottom attributes to be equal to that of their parent view which itself must be "full screen".

You probably need to set the contentMode of the UIImageView to UIViewContentModeScaleAspectFill too. E.g.

imageView.contentMode = UIViewContentModeScaleAspectFill;

More information on contentMode can be found in the apple documentation for UIView

iOS 8 - Background gradient for launch screen file

You cannot run code or use any custom classes in the Launch Screen.

You'll have to supply a stretchable image, which contains enough data for it to be viable in all resolutions.

Also, you could take advantage of the @2x and @3x modifiers.
iPhone 6 plus will try to load a @3x modifier, AFAIK.

As a last resort, if the Launch Screen xib is not enough for you, you could still use the UILaunchImages plist key, and specify images for minimum version of 8.0. The downside is that Xcode does not automatically generate those for you, so you'll have to write those manually. Also remember that the modifier for iPhone 6 plus images is @3x

An example:

UILaunchImage = Default // This is for iOS 6, if you need it

UILaunchImages // iOS 7, 8
- [0]
- UILaunchImageName = Default
- UILaunchImageMinimumOSVersion = 7.0
- UILaunchImageSize = {320, 480}
- UILaunchImageOrientation = Portrait
- [1]
- UILaunchImageName = Default-568h
- UILaunchImageMinimumOSVersion = 7.0
- UILaunchImageSize = {320, 568}
- UILaunchImageOrientation = Portrait
- [2]
- UILaunchImageName = Default-667h
- UILaunchImageMinimumOSVersion = 8.0
- UILaunchImageSize = {375, 667}
- UILaunchImageOrientation = Portrait
- [3]
- UILaunchImageName = Default-736h
- UILaunchImageMinimumOSVersion = 8.0
- UILaunchImageSize = {414, 736}
- UILaunchImageOrientation = Portrait

P.S. The plist above is also a perfect solution for those who are struggling with the localization of Image Catalogs or Launch Screen files.



Related Topics



Leave a reply



Submit