Use masonry to realize keyboard retract and eject

Source: Internet
Author: User

First of all, say the following:
I typically use a constraint of a numeric type with Mas_equalto, and instead of a control, or a constraint on a control, I'm using Equalto, such as:
Make.size.mas_equalTo (Cgsizemake (100, 100));
Make.center.equalTo (Weakself.view);
Setneedslayout: Tells the page to be updated, but does not start the update immediately. Layoutsubviews is called immediately after execution.
layoutifneeded: Tells the page layout to be updated immediately. So the general will be used with setneedslayout. If you want to call this method to generate a new frame immediately, use this general layout animation to make the animation take effect directly after the layout is updated.
Layoutsubviews: System Rewrite layout
Setneedsupdateconstraints: Notifies you of the need to update the constraint, but does not start immediately
updateconstraintsifneeded: Notify update constraint immediately
Updateconstraints: System Update constraint
-(void) updateviewconstraints Viewcontroller The Updateviewconstraints method of Viewcontroller is called when the view layout is updated. Instead of inheriting the view to rewrite the-updateconstraints method, we can override this method to update the internal layout of the current view. When we override this method, be sure to call super or call the-updateconstraints method of the current view.

1. View Center Display
Prevent circular references in block
__weak typeof (self) weakself = self;
uiview* view = [UIView new];
View.backgroundcolor = [Uicolor Browncolor];
[Self.view Addsubview:view];
Adding a constraint using mas_makeconstraints
[View mas_makeconstraints:^ (Masconstraintmaker *make) {

Add size constraint (make is the control view to which you want to add a constraint)
Make.size.mas_equalTo (Cgsizemake (200, 200));

Add a center constraint (centered in the same way as self)
Make.center.equalTo (Weakself.view);
}];
2. Two views equal width high margin
uiview* Blackview = [UIView new];
Blackview.backgroundcolor = [Uicolor blackcolor];
[Self.view Addsubview:blackview];

[Blackview mas_makeconstraints:^ (Masconstraintmaker *make) {
Add Constraint size
Make.size.mas_equalTo (Cgsizemake (100, 100));
On the left, add the constraint (both the left and top constraints are 20)
Make.left.and.top.mas_equalTo (20);
}];

uiview* Grayview = [UIView new];
Grayview.backgroundcolor = [Uicolor Lightgraycolor];
[Self.view Addsubview:grayview];

[Grayview mas_makeconstraints:^ (Masconstraintmaker *make) {
Size, top margin constraint is the same as Black view
Make.size.and.top.equalTo (Blackview);
Add a right margin constraint (the spacing here is directional, the left and top margins are positive, the right and bottom margins are negative)
Make.right.mas_equalTo (-20);
}];
3. Keyboard Popup and retract
-(void) Dealloc {
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}

-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.
__weak typeof (self) weakself = self;
_textfield = [Uitextfield new];
_textfield.backgroundcolor = [Uicolor Redcolor];
[Self.view Addsubview:_textfield];

[_textfield mas_makeconstraints:^ (Masconstraintmaker *make) {
Left,right,centerx,y can't coexist, there's only one second.
Make.left.mas_equalTo (20);
Make.right.mas_equalTo (-60);
Make.centerX.equalTo (Weakself.view);
Make.height.mas_equalTo (40);
Make.bottom.mas_equalTo (0);
}];

Registering keyboard notifications
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillchangeframenotification:) Name:uikeyboardwillchangeframenotification Object:nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillhidenotification:) Name: Uikeyboardwillhidenotification Object:nil];
}

-(void) Keyboardwillchangeframenotification: (nsnotification *) Notification {

Get keyboard Basics (animation duration vs. keyboard height)
Nsdictionary *userinfo = [notification UserInfo];
CGRect rect = [Userinfo[uikeyboardframebeginuserinfokey] cgrectvalue];
CGFloat keyboardheight = cgrectgetheight (rect);
CGFloat keyboardduration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];
Modify the bottom margin constraint
[_textfield mas_updateconstraints:^ (Masconstraintmaker *make) {
Make.bottom.mas_equalTo (-keyboardheight);
}];

Update constraint
[UIView animatewithduration:keyboardduration animations:^{
[Self.view layoutifneeded];
}];
}

-(void) Keyboardwillhidenotification: (nsnotification *) Notification {

Get Keyboard animation duration
Nsdictionary *userinfo = [notification UserInfo];
CGFloat keyboardduration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];

Modify to Previous constraint (0 from bottom margin)
[_textfield mas_updateconstraints:^ (Masconstraintmaker *make) {
Make.bottom.mas_equalTo (0);
}];

Update constraint
[UIView animatewithduration:keyboardduration animations:^{
[Self.view layoutifneeded];
}];
}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {
[Super Touchesbegan:touches Withevent:event];
[Self.view Endediting:yes];
}
4. Three controls, etc. wide spacing
Method One:

The Mas_distributeviewsalongaxis withfixedspacing of an array changes the length or width of the control
Defines an array that holds three controls Nsarray *array;
Array = @[greenview,redview,blueview];
Call the following method directly:
-(void) Gethorizontalone
{
Method One, the Mas_distributeviewsalongaxis of the array
/**
* Constant interval arrangement of multiple controls at fixed intervals, changing the length or width of a control
*
* @param axistype Axis Direction
* @param fixedspacing interval size
* @param leadspacing head Spacing
* @param tailspacing tail interval
*/
Masaxistypehorizontal level
Masaxistypevertical Vertical

[ArrayList Mas_distributeviewsalongaxis:masaxistypehorizontal
Withfixedspacing:20
Leadspacing:5
Tailspacing:5];
[ArrayList mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.top.mas_equalTo (60);
Make.height.mas_equalTo (100);
}];
}
Method Two:

Array de Mas_distributeviewsalongaxis withfixeditemlength control size unchanged, gap is changed
-(void) getvertical
{
/**
* Constant interval arrangement of multiple fixed-size controls, changing the gap between intervals
*
* @param axistype Axis Direction
* @param fixeditemlength fixed length or width value for each control
* @param leadspacing head Spacing
* @param tailspacing tail interval
*/
[ArrayList mas_distributeviewsalongaxis:masaxistypevertical
Withfixeditemlength:60
Leadspacing:40
TAILSPACING:10];
[ArrayList mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.top.mas_equalTo (100);
Make.height.mas_equalTo (100);
Make.left.mas_equalTo (20);
Make.right.mas_equalTo (-20);
}];
}
Both of these methods are in the nsarray+masadditions.


Method Three: Directly set multiplier realization equal spacing

for (Nsuinteger i = 0; i < 4; i++) {
UIView *itemview = [self getitemviewwithindex:i];
[_containerview Addsubview:itemview];

[Itemview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.width.and.height.equalTo (@ (item_size));
Make.centerY.equalTo (_containerview.mas_centery);
Make.centerX.equalTo (_containerview.mas_right). Multipliedby (((cgfloat) i + 1)/((cgfloat) Item_count + 1));
}];
}
Method Four: Realize equal spacing by using spaceview of transparent width

UIView *lastspaceview = [UIView new];
Lastspaceview.backgroundcolor = [Uicolor Greencolor];
[_containerview1 Addsubview:lastspaceview];

[Lastspaceview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.and.top.and.bottom.equalTo (_CONTAINERVIEW1);
}];

for (Nsuinteger i = 0; i < Item_count; i++) {
UIView *itemview = [self getitemviewwithindex:i];
[_containerview1 Addsubview:itemview];

[Itemview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.height.and.width.equalTo (@ (item_size));
Make.left.equalTo (Lastspaceview.mas_right);
Make.centerY.equalTo (_containerview1.mas_centery);
}];

UIView *spaceview = [UIView new];
Spaceview.backgroundcolor = [Uicolor Greencolor];
[_containerview1 Addsubview:spaceview];

[Spaceview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.equalTo (itemview.mas_right). With.priorityhigh (); Reduce precedence to prevent constraint conflicts from being insufficient
Make.top.and.bottom.equalTo (_CONTAINERVIEW1);
Make.width.equalTo (Lastspaceview.mas_width);
}];

Lastspaceview = Spaceview;
}

[Lastspaceview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.right.equalTo (_containerview1.mas_right);
}];
5. Dynamically changing the font width

and Polygon Method 4, use Spaceview to implement
uiview* Bgview = [[UIView alloc]init];
Bgview.backgroundcolor = [Uicolor Yellowcolor];
[Self.view Addsubview:bgview];

[Bgview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.and.right.mas_equalTo (0);
Make.top.mas_equalTo (@100);
Make.height.mas_equalTo (@100);
}];

Listtext = @[@ "Beijing", @ "big Richthoff ah", @ "your uncle", @ "Our love Hey Hey"];
UIView *lastspaceview = nil;
for (int i = 0; i < Listtext.count; i + +)
{
uilabel* label = [UILabel new];
Label.text = Listtext[i];
Label.backgroundcolor = Randomcolor;
[Bgview Addsubview:label];

uiview* Lineview = [UIView new];
Lineview.backgroundcolor = [Uicolor Redcolor];
[Bgview Addsubview:lineview];

[Label mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.top.bottom.mas_equalTo (0);
if (Lastspaceview)
{
NSLog (@ "presence Lastview");
Make.left.equalTo (lastspaceview.mas_right). Mas_offset (@20);
}else
{
NSLog (@ "does not exist Lastview");
Make.left.equalTo (Bgview.mas_left);
}
Make.height.equalTo (Bgview);
}];

Lastspaceview = label;

[Lineview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.top.and.bottom.mas_equalTo (0);
Make.width.mas_equalTo (1);
Make.left.mas_equalTo (label.mas_right). Mas_offset (@10);
}];
}
:


6. The height of the parent view is the height of the two controls and

uiview* Bgview = [UIView new];
Bgview.backgroundcolor = [Uicolor Purplecolor];
[Self.view Addsubview:bgview];

uilabel* Titlelab = [UILabel new];
Titlelab.backgroundcolor = [Uicolor Redcolor];
Titlelab.textalignment = Nstextalignmentcenter;
Titlelab.font = [Uifont systemfontofsize:15.f];
Titlelab.text = @ "Caocao-the short song line";
[Bgview Addsubview:titlelab];

uilabel* Contentlab = [UILabel new];
Contentlab.numberoflines = 0;
Contentlab.textalignment = Nstextalignmentcenter;
Contentlab.backgroundcolor = [Uicolor Browncolor];
Contentlab.font = [Uifont systemfontofsize:13.f];
Contentlab.text = @ "Complaining, life geometry?" For example, the morning dew, daycare bitter. \ Tang Dang, I remember. Why melancholy? Only a Dukang. \ Geumcheon Green son, leisurely my heart. But for the sake of June, pondered so far. \ Yo yo deer, eat wild apple. I have a guest, the drum blowing sheng. \ duo When the Moon is clear? It is not to be severed from sorrow. \ n The more Qian, the more it is in vain. The nostalgic of the feast, the heart of the grace. \ Yuemingxingxi, the Black Magpie flew south. Three turns around the tree, what branch can be? The mountain is not too high, the sea is too deep. Zhou Gong, Center. ";

[Bgview Addsubview:contentlab];
Idea: The upper space on the parent view is equal to the top space of title, and the lower space of the parent view is equal to the bottom space of content
__weak typeof (self) weakself = self;
[Bgview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.mas_offset (@30);
Make.right.mas_offset (@-30);
Make.centerY.equalTo (Weakself.view);
}];

[Titlelab mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.top.right.mas_equalTo (@0);
}];

[Contentlab mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.right.mas_equalTo (@0);
Make.top.equalTo (Titlelab.mas_bottom). Mas_offset (@10);
Make.bottom.equalTo (Bgview);
}];
:

Later slowly update, record convenient later use



Wen/Dong Fei
Some other people's records.
Adaptive layouts allow you to set the width or height to a fixed value. If you want to give the view a minimum or maximum value, you can do this:
Width >= && width <= 400make.width.greaterthanorequalto (@200); Make.width.lessThanOrEqualTo (@400)
Precedence of constraints
The. Priority allows you to specify a precise priority, and the higher the value the greater the precedence. up to 1000.
The. Priorityhigh is equivalent to Uilayoutprioritydefaulthigh. The priority value is 750.
. Prioritymedium between high and low priority, the priority value is between 250~750.
The. Prioritylow is equivalent to Uilayoutprioritydefaultlow, with a priority value of 250.
The priority can be added at the end of the constraint:
Make.left.greaterThanOrEqualTo (Label.mas_left). With.prioritylow ();
Make.top.equalTo (label.mas_top). with.priority (600);
Center Centre
Make CenterX and centery = button1
Make.center.equalTo (button1)
Make CenterX = superview.centerx-5, CenterY = Superview.centery + 10make.center.equalto (superview). CenterOffset ( Cgpointmake (-5, 10))
Specifies a width of 1/4 for the parent view.
Make.width.equalTo (Superview). Multipliedby (0.25);

Use masonry to realize keyboard retract and eject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.