iOS 8 Widget Alignment Issue - ios

I'm having trouble aligning an iOS 8 widget all the way to the left. It seems that setting the x origin to 0 still keeps a certain amount of space between the left edge of the screen and my first view.
I'm not sure how Evernote does this, but it seems that they have it figured out. Any suggestions? I also tried setting the x position programmatically to no success.

Here the answer:
// MARK: NCWidgetProviding protocol methods
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets
{
return UIEdgeInsetsZero
}

Related

SafeArea backwards compatibility

I'm having an issue when trying to layout a view programatically and I cant seem to find a concise, non hacky way to fix it.
I'm using safeAreaInsets to size some elements in my view. This works well until I try it out on a pre iOS 11 device. Obviously, with the lack of safeAreaInsets the sizing of my subviews falls apart and everything becomes a mess. What do I fall back to when using older versions of iOS.
More specifically, what can I implement in the extension below that will work as expected?
extension UIView {
func compatibilityInsets() -> UIEdgeInsets {
if #available(iOS 11.0, *) {
return self.safeAreaInsets
} else {
//what goes here?
return self.olderVersionOfInsets
}
}
}
Here is an exmaple of how I might use this extension method:
var minimumHeaderHeight: CGFloat {
//allows the header height to be 70 below navigation bar
return 70 + view.compatibilityInsets().top
}
safeAreaInsets was added to help avoid content disappearing behind the "notch" in the iPhone X... which only supports iOS 11 IIRC.
So the alternative for iOS 10 and below would be return .zero as there doesn't need to be any safe area defined.
.zero in this case is inferred to be of type UIEdgeInsets so is equivalent to calling UIEdgeInsets.zero.

How to debug layout with Multiline UILabel / autolayout in notification content extension

How to debug the following issue? Is there a way how to work around this issue?
There seems to be a bug in iOS 10.2 and below when laying out a multi-line UILabel.
I have a fairly simple UIView subclass which I use in both app and notification content extension, that looks like this:
In the main app, everything is laid out just fine:
When shown in notification content extension on iOS 10.2 and below, the layout is broken. But only when the text is long enough to be broken into multiple lines. Seems like iOS can't calculate correct height of the whole view:
However, this issue seems to be fixed on iOS 10.3 and newer:
I started experimenting with the subviews, specifically by setting fixed height constraints.
Turns out, it was not the label(s) that caused the issue with calculating overall height but the aspect ratio constraint (width:height) on the topmost view.
Programmatically calculating height based on the view's width and setting a height constraint for the affected view helped to fix the issue:
public override func updateConstraints() {
super.updateConstraints()
if #available(iOS 10.2, *) {
imageContainerHeightConstraint.isActive = false
} else {
// FIX: multiline label / aspect ratio / autolayout bug in iOS < 10.2
let ratio: CGFloat = imageContainerAspectRatioConstraint.multiplier
imageContainerHeightConstraint.constant = round(bounds.width/ratio)
imageContainerHeightConstraint.isActive = true
}
}

UITextView scroll bug iOS9 xcode 7.1.1

I have a simple UITextView that occupies all the screen.
When the text length is bigger than the Textview height, it automatically scrolls to the bottom (see image).
I have already tried
self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
and
self.textView.scrollsToTop = true
and other things without any result.
I'm using iOS 9 with the latest swift and Xcode 7.1.1.
I've found a solution.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.textView.setContentOffset(CGPoint.zero, animated: false)
}
but I still don't understand why it didn't work.
You also try
self.textView.scrollRangeToVisible(NSRange(location:0, length:0))

CollectionView margin not the same

i'm trying to create a simple collectionView like pinterest. I've reached a problem i've set the margins like in the image, but as u can see the middle margin is 20 since both right and left is 10 how can i make so that its also 10. i've tried changing some values, but it is not working.
for fitting all orientations
Is there a better way?
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
return CGSizeMake(self.collectionView!.frame.width/2-20, self.collectionView!.frame.width/2-20+50)
}
Use auto layout and view constraints. This will allow you to have each tile maintain its spacing and scale for different orientations.
see the apple docs

How to make an iOS-App Extension for the Notification Center full width?

I am currently trying to develop my own iOS-App Extension for the Notification-Center but there is a problem with the width. I can not change my widget to be full width like the Calender-Extension on the screenshot. There is always some space to the left that cuts of my Viewcontroller content. (See the Number Widget at the second Screenshot) It seems this is the default behaviour, but there must be a way around this. Any Ideas how to solve this Issue ?
You have to do this:
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
Taken from Today Extension Not Positioned Correctly
In your main extension view controller, override - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets and return whatever UIEdgeInsets your extension needs. If you want 0 padding on all sides, simply return UIEdgeInsetsZero.

Resources