2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AutoLayoutのうるさい警告をもみ消す

2
Posted at

iOS開発でAutoLayoutをつかって開発していると、実行時のログに

Unable to simultaneously satisfy constraints.
	Probably at least one of the constraints in the following list is one you don't want.
(以下略)

みたいな警告が出ることがあります。
本来は警告が出ないようにするのがベストですが、「うるせぇ、動いてるからいいんじゃ!」とか「そんなのを修正する暇があったらもっとやることがある」とかで無視したい場合もあるでしょう。

しかしこの警告はとても長く、複数回出たりするのでログが一瞬で流れていって不便です。(しかも例外を投げているのでデバッガでデバッグ中にいちいち止められることも)

この警告はUIViewのengine:willBreakConstraint:dueToMutuallyExclusiveConstraints: で出る警告なので、このメソッドを差し替えて、警告がでないようにできます。

UIView+Layout.h
# import <UIKit/UIKit.h>

@interface UIView (Layout)
@end
UIView+Layout.m
# import "UIView+Layout.h"
# import <objc/runtime.h>

@interface UIView (LayoutHidden)
-(void)engine:(id)engine willBreakConstraint:(id)constraint dueToMutuallyExclusiveConstraints:(id)mutuallyExclusiveConstraints;
@end

@implementation UIView (Layout)
-(void)engineDummy:(id)engine willBreakConstraint:(id)constraint dueToMutuallyExclusiveConstraints:(id)mutuallyExclusiveConstraints{
}

+ (void)load{
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:)),
                                   class_getInstanceMethod(self, @selector(engineDummy:willBreakConstraint:dueToMutuallyExclusiveConstraints:)));
}
@end
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?