Upvote:1
Aprove answer
UIButton *button1 = [[UIButton alloc] init];
button1.translatesAutoresizingMaskIntoConstraints = FALSE;
[button1 setTitle:@"Button 1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blueColor];
UIButton *button2 = [[UIButton alloc] init];
button2.translatesAutoresizingMaskIntoConstraints = FALSE;
[button2 setTitle:@"Button 2" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor brownColor];
// Add a container view to hold two buttons
UIView *containerView = [[UIView alloc] init];
containerView.translatesAutoresizingMaskIntoConstraints = FALSE;
[containerView addSubview:button1];
[containerView addSubview:button2];
// vertical constraints
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button1]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button1)]];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button2]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button2)]];
// horizontal constraints
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button1]-(30)-[button2]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button1, button2)]];
// Add the container view with centerx and centery constraints to
// keep the buttons in the center
[self.view addSubview:containerView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
Upvote:1
self.view.backgroundColor = [UIColor redColor];
UIStackView*st = [UIStackView new];
st.distribution = UIStackViewDistributionFillEqually;
st.axis = UILayoutConstraintAxisHorizontal;
st.spacing = 40;
UIButton*b1 = [UIButton buttonWithType:UIButtonTypeSystem];
UIButton*b2 = [UIButton buttonWithType:UIButtonTypeSystem];
[b1 setTitle:@"ClickB1" forState:UIControlStateNormal];
[b2 setTitle:@"ClickB2" forState:UIControlStateNormal];
st.translatesAutoresizingMaskIntoConstraints = NO;
b1.translatesAutoresizingMaskIntoConstraints = NO;
b2.translatesAutoresizingMaskIntoConstraints = NO;
[st addArrangedSubview:b1];
[st addArrangedSubview:b2];
[self.view addSubview:st];
[[st.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor] setActive:YES];
[[st.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor] setActive:YES];
Credit Goes to: stackoverflow.com
Related question with same questions but different answers
- OBJECTIVE C: CENTER WITH NSLAYOUTCONSTRAINT IN IOS
- VUEJS: PREVENT BOUNCING AND ZOOMING ON IOS10+ WITH ROUTING IN IOS
- IMPLEMENTING COLOR TRANSFER BETWEEN TWO IMAGES WITH A CORE IMAGE CUSTOM FILTER IN IOS
- FIREBASE CONNECTING WITH SIMULATOR BUT NOT IN REAL DEVICE IOS IN IOS
- WHERE CLAUSE IN EXTENSION OF A CLASS IN SWIFT WITH ENUM AND GENERICS IN IOS
- DISPLAYING LEADERBOARD USING UITABLEVIEWCONTROLLER WITH FIREBASE BACKEND (SWIFT 4.1) IN IOS
- DISTRIBUTION FAILED WITH XCODE 10 BETA 4 - [ERROR ITMS-90534] IN IOS
- SCROLLVIEW WITH AUTOLAYOUT NOT WORKING IN IOS
- XAMARIN FORMS: ISSUE WITH CREATING DISTRIBUTION CERTIFICATE AND .P12 FILE IN IOS
- API SWIFT 4 CALL WITH AUTHENTICATION. SIMPLE. BUT STUCK IN IOS
- IOS IMAGE VIEWER OR PREVIEW EXACT LIKE PHOTOS APP USING COLLECTION VIEW (PAGING ENABLED) WITH MINIMUM LINE SPACING IN IOS
- SWIFT AND SPRITEKIT: HOW TO IMPLEMENT NON-FUZZY CIRCLE TIMER WITH SKSHAPENODE IN IOS
- HOW TO PARSE AND ARRAY OF ARRAYS WITH ARRAYS IN SWIFTYJSON? IN IOS
- HOW TO SHARE TO FACEBOOK WITH A LINK BACK TO THE APP? SWIFT IN IOS
- KEEP THE HEIGHT/WIDTH OF UITEXTFIELD THE SAME ON IPAD AND IPHONE WITH AUTOLAYOUT IN IOS
- BOOTSTRAP 4 TABS NOT WORKING ON IOS WITH "DATA-TARGET" INSTEAD OF "HREF" ATTRIBUTES IN IOS
- UITABLEVIEW WITH SECTIONS FROM A LOCAL JSON FILE IN IOS
- BAR BUTTON ITEM IMAGE (UIIMAGE) COLOR CHANGES WITH TINT COLOR? IN IOS
- EVER SCROLLING UISCROLLVIEW WITH UIIMAGE IN IOS
- HOW TO CHANGE TAB BAR PROGRAMMATICALLY WITH ANIMATION? IN IOS
- RUNNING REACT-NATIVE APP ON LOCAL IOS DEVICE WITH FASTLANE MATCH METHOD IN IOS
- CALCULATING HEIGHT OF UICOLLECTIONVIEWCELL WITH TEXT ONLY IN IOS
- SHARING BUNDLE IDENTIFIER WITH OWN SERVER OR THIRD PARTY SERVER IN IOS
- PARSE CLOUD WITH SWIFT PFIDRESULTBLOCK ERROR IN IOS
- CANNOT SUBSCRIPT A VALUE OF TYPE NSDICTIONARY WITH AN INDEX OF TYPE STRING. WHILE CONVERTING FROM SWIFT 2.3 -> 3.2 IN IOS
- CONVERT DATE WITH +1000 VALUES IN IOS
- CAN I BUILD AN IPA WITH AN I386 + X86_64 ARCHITECTURE? IN IOS
- CAN NOT READ THE SPECIFIC VALUES OF JSON FILE IN OBJECTIVE C IN IOS
- UITABLEVIEW WITH IMAGES SCROLLS VERY SLOWLY XAMARIN.IOS IN IOS
- UNABLE TO SAVE INTEGER WITH NSUSERDEFAULTS IN IOS
- FIREBASE DATABASE READ CHILDBYAUTOID IN IOS
- MAKE ALL SUBCLASSES CONFORM TO A PARTICULAR PROTOCOL IN IOS
- UITEXTFIELD WONT DISMISS IN IOS
- HOW TO UPLOAD DOCUMENT LIKE PDF , .TXT FROM IPHONE PROGRAMATICALLY IN SWIFT? IN IOS
- HOW CAN I BINDING SFMASKEDEDIT FROM SYNCFUSION IN XAMARIN? IN IOS
- REACT NATIVE: "SETBACKGROUNDCOLOR IS ONLY AVAILABLE ON ANDROID" WARNING ON IOS IN IOS
- IN-APP PURCHASE IN APPLICATION IN IOS
- CHECK IF AN IOS DEVICE WAS BLOCKED IN IOS
- HOW TO STOP TABBAR ITEMS FROM TURNING GREY WHEN POPOVER APPEARS IN IOS
- FIRESTORE IOS - DELETE FUNCTION IN UITABLEVIEW IN IOS
- UIVIEW.ANIMATE WITH ALLOWUSERINTERACTION ENABLES THE UIBUTTON ONLY IN THE ANIMATION'S FINAL FRAME IN IOS
- TABLEVIEW CELL ACTION ISSUE XCODE IN IOS
- HOW TO HANDLE SWITCH PAGE LOGIC ON BACK PRESS IN FLUTTER IN IOS
- OPENING MULTIPLE DOCUMENTS IN A UIDOCUMENTBROWSERVIEWCONTROLLER BASED APP IN IOS
- LAYOUTIFNEEDED DOES NOT TRIGGER LAYOUTSUBVIEWS IN IOS
- HOW TO ADD GMSMAPVIEW TO A PORTION OF A VIEW IN SWIFT 4 IN IOS
- HOW DOES REACT NATIVE KNOW MY DEFAULT CLASS NAME? IN IOS
- NOTIFYING IOS USER THAT BLUETOOTH MUST BE TURNED ON IN IOS
- HERE CORNER IS WORKING BUT SHADOW IS NOT WORK IN IOS 9 AND 10 IN IOS
- HOW TO CONVERT STRING TO INT WITHOUT SKIP FIRST ZEROS'S SWIFT? IN IOS
- SWIFT - HOW TO DISABLE CLICKED HISTORY ON IOS TABLEVIEW IN IOS
- APPLE MACH-O LINKER ERROR SIRI TARGET INTENTS EXTENSION ADD ON IN IOS
- CGAFFINETRANSFORM NOT ANIMATING WITHIN ANIMATION BLOCK IN IOS
- IF PUSH NOTIFICATION CERTIFICATE EXPIRED THEN ALL EXISTING USER STOP GETTING NOTIFICATION? IN IOS
- UNWRAPPING ERROR IN OPTIONAL VALUE IN FILE PATH IN IOS
- UICOLLECTIONVIEW DISAPPEARS IN UISTACKVIEW IN IOS
- NO RESULTS LABEL ON EMPTY TABLEVIEW IN IOS
- ROWS COUNT IN SECTION IN IOS
- CANNOT CONVERT VALUE OF TYPE STRING TO SPECIFIED TYPE NSMANAGEDOBJECTCONTEXT, WHILE CONVERTING FROM SWIFT 2.3 -> 3.2 IN IOS
- SWIFT: SHOW DATA FROM TABLEVIEW TO ANOTHER VIEWCONTROLLER (JSON, ALAMORIFE, ALAMOFIREIMAGE) IN IOS