Jasne! Poniżej przygotowałem kompletny, prosty projekt iOS w Xcode, który odtwarza interfejs kalkulatora z wykorzystaniem Storyboard, Auto Layout i Stack View. Aplikacja nie wykonuje żadnych obliczeń — tylko wyświetla UI, które dobrze działa na różnych rozmiarach ekranu i w orientacji poziomej.
Jak utworzyć projekt w Xcode
- Otwórz Xcode i wybierz File > New > Project.
- Wybierz App pod iOS.
- Nazwij projekt np.
CalculatorUI.
- Jako Interface wybierz Storyboard.
- Jako Language wybierz Swift.
- Utwórz projekt.
Struktura projektu
Main.storyboard — zawiera UI kalkulatora.
ViewController.swift — kontroler widoku (pusty, bez logiki).
1. Kod ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Brak logiki, UI jest w Storyboardzie
}
}
2. Storyboard — opis i instrukcje krok po kroku
Cel:
Stworzyć UI kalkulatora z:
- Ekranem wyświetlacza na górze (UILabel)
- 5 rzędami przycisków (4 przyciski w każdym rzędzie)
- Użycie Stack View do układu przycisków i całego layoutu
- Auto Layout, aby UI dobrze działało na różnych ekranach i orientacjach
Krok po kroku w Storyboard:
-
Usuń domyślny ViewController i dodaj nowy ViewController (lub użyj domyślnego).
-
Dodaj UIView (główny kontener) do widoku kontrolera, rozciągnij go na cały ekran (ustaw constraints: 0 do góry, do dołu, do lewej i do prawej).
-
Dodaj pionowy Stack View (UIStackView) do głównego widoku, rozciągnij go na cały ekran z marginesem 16 pt z każdej strony.
- Axis: Vertical
- Distribution: Fill
- Alignment: Fill
- Spacing: 8
-
Dodaj UILabel na górę Stack View — to będzie wyświetlacz kalkulatora.
- Text: "0"
- Font: System Bold, size 48
- Text Alignment: Right
- Number of lines: 1
- Background color: light gray (np. systemGray5)
- Height constraint: 100 pt
-
Dodaj 5 poziomych Stack View (UIStackView) do pionowego Stack View — każdy poziomy Stack View będzie rzędem przycisków.
- Axis: Horizontal
- Distribution: Fill Equally
- Alignment: Fill
- Spacing: 8
-
Do każdego poziomego Stack View dodaj 4 UIButton.
-
Ustaw tytuły przycisków zgodnie z typowym kalkulatorem:
- Rząd 1: "AC", "+/-", "%", "÷"
- Rząd 2: "7", "8", "9", "×"
- Rząd 3: "4", "5", "6", "−"
- Rząd 4: "1", "2", "3", "+"
- Rząd 5: "0", ".", "="
Uwaga: W ostatnim rzędzie przycisk "0" powinien zajmować dwie kolumny (czyli być szerszy).
Jak zrobić przycisk "0" szerszy?
- W ostatnim poziomym Stack View dodaj 3 przyciski: "0", ".", "=".
- Ustaw
Distribution na Fill Proportionally.
- Ustaw szerokość przycisku "0" na dwukrotność pozostałych (np. dodaj constraint szerokości "0" = 2 * szerokości "." lub "=").
Stylizacja przycisków
- Tło: jasnoszare dla cyfr i funkcji, pomarańczowe dla operatorów (÷, ×, −, +, =)
- Zaokrąglone rogi (cornerRadius = 10)
- Font: system bold, size 28
- Kolor tekstu: czarny dla cyfr i funkcji, biały dla operatorów
3. Kompletny plik Main.storyboard
Poniżej znajdziesz uproszczony XML Storyboard, który możesz wkleić do pliku Main.storyboard w projekcie Xcode (upewnij się, że usuniesz istniejący kod Storyboard i zastąpisz go tym):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="NO" useTraitCollections="YES" colorMatched="YES" initialViewController="VC">
<scenes>
<!--View Controller-->
<scene sceneID="scene1">
<objects>
<viewController id="VC" customClass="ViewController" customModule="CalculatorUI" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="view1">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fill" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="mainStack">
<rect key="frame" x="16" y="16" width="382" height="864"/>
<subviews>
<!-- Display Label -->
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" text="0" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="displayLabel">
<rect key="frame" x="0" y="0" width="382" height="100"/>
<fontDescription key="fontDescription" type="system" pointSize="48" weight="bold"/>
<color key="textColor" systemColor="labelColor"/>
<color key="backgroundColor" systemColor="systemGray5Color"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
<textAlignment key="textAlignment" value="right"/>
</label>
<!-- Row 1 -->
<stackView opaque="NO" contentMode="scaleToFill" axis="horizontal" distribution="fillEqually" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="row1">
<subviews>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnAC">
<rect key="frame" x="0" y="0" width="88" height="64"/>
<state key="normal" title="AC">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnPlusMinus">
<rect key="frame" x="96" y="0" width="88" height="64"/>
<state key="normal" title="+/-">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnPercent">
<rect key="frame" x="192" y="0" width="88" height="64"/>
<state key="normal" title="%">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnDivide">
<rect key="frame" x="288" y="0" width="88" height="64"/>
<state key="normal" title="÷">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemOrangeColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
</stackView>
<!-- Row 2 -->
<stackView opaque="NO" contentMode="scaleToFill" axis="horizontal" distribution="fillEqually" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="row2">
<subviews>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn7">
<rect key="frame" x="0" y="0" width="88" height="64"/>
<state key="normal" title="7">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn8">
<rect key="frame" x="96" y="0" width="88" height="64"/>
<state key="normal" title="8">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn9">
<rect key="frame" x="192" y="0" width="88" height="64"/>
<state key="normal" title="9">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnMultiply">
<rect key="frame" x="288" y="0" width="88" height="64"/>
<state key="normal" title="×">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemOrangeColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
</stackView>
<!-- Row 3 -->
<stackView opaque="NO" contentMode="scaleToFill" axis="horizontal" distribution="fillEqually" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="row3">
<subviews>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn4">
<rect key="frame" x="0" y="0" width="88" height="64"/>
<state key="normal" title="4">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn5">
<rect key="frame" x="96" y="0" width="88" height="64"/>
<state key="normal" title="5">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn6">
<rect key="frame" x="192" y="0" width="88" height="64"/>
<state key="normal" title="6">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnMinus">
<rect key="frame" x="288" y="0" width="88" height="64"/>
<state key="normal" title="−">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemOrangeColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
</stackView>
<!-- Row 4 -->
<stackView opaque="NO" contentMode="scaleToFill" axis="horizontal" distribution="fillEqually" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="row4">
<subviews>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn1">
<rect key="frame" x="0" y="0" width="88" height="64"/>
<state key="normal" title="1">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn2">
<rect key="frame" x="96" y="0" width="88" height="64"/>
<state key="normal" title="2">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn3">
<rect key="frame" x="192" y="0" width="88" height="64"/>
<state key="normal" title="3">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnPlus">
<rect key="frame" x="288" y="0" width="88" height="64"/>
<state key="normal" title="+">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemOrangeColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
</stackView>
<!-- Row 5 -->
<stackView opaque="NO" contentMode="scaleToFill" axis="horizontal" distribution="fillProportionally" alignment="fill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="row5">
<subviews>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btn0">
<rect key="frame" x="0" y="0" width="176" height="64"/>
<state key="normal" title="0">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnDot">
<rect key="frame" x="184" y="0" width="88" height="64"/>
<state key="normal" title=".">
<color key="titleColor" systemColor="labelColor"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="btnEqual">
<rect key="frame" x="280" y="0" width="88" height="64"/>
<state key="normal" title="=">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<fontDescription key="fontDescription" type="system" pointSize="28" weight="bold"/>
<color key="backgroundColor" systemColor="systemOrangeColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius" value="10"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="clipsToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="mainStack" firstAttribute="top" secondItem="view1" secondAttribute="top" constant="16" id="c1"/>
<constraint firstItem="mainStack" firstAttribute="leading" secondItem="view1" secondAttribute="leading" constant="16" id="c2"/>
<constraint firstItem="mainStack" firstAttribute="trailing" secondItem="view1" secondAttribute="trailing" constant="-16" id="c3"/>
<constraint firstItem="mainStack" firstAttribute="bottom" secondItem="view1" secondAttribute="bottom" constant="-16" id="c4"/>
<constraint firstItem="displayLabel" firstAttribute="height" constant="100" id="c5"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="firstResponder" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
4. Podsumowanie
- Projekt zawiera prosty kalkulator UI z 5 rzędami przycisków i wyświetlaczem.
- Użyto Stack View i Auto Layout, aby UI było responsywne.
- Przyciski mają zaokrąglone rogi i odpowiednie kolory.
- Przycisk "0" jest dwukrotnie szerszy.
- Brak logiki obliczeń — tylko UI.
Jeśli chcesz, mogę pomóc Ci krok po kroku w Xcode lub wygenerować gotowy projekt ZIP do pobrania. Daj znać!