Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Background Colors and Background Image Example (iPhone,iPad)

iOS/iPhone Background Colors and Background Image Example (iPhone,iPad) บทความนี้เราจะมาเขียน iOS ทำการแสดงผลบน iPhone การสร้าง View Controller บน Xcode และการเปลี่ยนสี Background Color (สีพื้นหลัง) และ Background Image (พื้นหลังแบบรูปแบบ) ทั้งใช้การปรับแต่งผ่าน Inspector ของ Xocde และการเขียนบน Objective-C ในการที่จะเรียก Image หรือรูปภาพต่าง ๆ มาแสดงเป็น Background

iOS/iPhone Background Colors and Background Image Example

iOS/iPhone Background Colors and Background Image Example


ใสตัวอย่างนี้จะแบ่งออกเป็น 2 ส่วนคือ ส่วนแรกจะเป็น Background Color (สีพื้นหลัง) ตัวนี้สามารถประบแต่งผ่าน Inspector ของ Xcode ได้ง่าย ๆ และส่วนที่สองจะเป็น Background Image (พื้นหลังแบบรูปแบบ) ซึ่งจะใช้การเขียน Code บนภาษา Objective-C ในกรที่จะอ่านรูปภาพ UIImage และแสดงผลเป็น Background

Background Color
self.view.backgroundColor = [UIColor redColor];


Background Image
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];


Example ตัวอย่างการสร้าง Background Colors และ Background Image แบบง่าย ๆ

iOS/iPhone Background Colors and Background Image Example

เริ่มต้นด้วยการสร้าง Project บน Xcode แบบง่าย ๆ ด้วย Single View Application

iOS/iPhone Background Colors and Background Image Example

เลือกและไม่เลือกดังรูป จากนั้นให้คลิกที่ Create

iOS/iPhone Background Colors and Background Image Example

ตอนนี้เราได้หน้าจอ View เปล่า ๆ ยังไม่มีอะไรทั้งสิ้น

iOS/iPhone Background Colors and Background Image Example

ในการปรับแต่ง Background สามารคลิกที่ View แล้วเลือก ปรับแต่ที่ Inspector ได้ในทันที

iOS/iPhone Background Colors and Background Image Example

มุมมองของ Color Picker บน Xcode

iOS/iPhone Background Colors and Background Image Example

หลังจากเปลี่ยนสีแล้วจะเห็นผลลัพธ์ทันทีบน Xcode








Screenshot

iOS/iPhone Background Colors and Background Image Example

ทดสอบการรันบน iOS Simulator กับการเปลี่ยนสีของ Background

iOS/iPhone Background Colors and Background Image Example

หรือจะเขียนไว้ที่ method ของ viewDidLoad()

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor redColor];
}


Screenshot

iOS/iPhone Background Colors and Background Image Example

สามารถเปลี่ยนสีของ Background ได้เช่นเดียวกัน

การเปลี่ยน Background Image เนื่องจากในขนาดของหน้าจอและความคมชัดของ iPhone แต่ล่ะรุ่นจะไม่เท่ากัน โดยเฉพาะรุ่นใหม่ ๆ จะเป็นจอแบบ Ratina ฉะนั้นขนาดรูปภาพที่ใช้คือ

320 x 480 (for iPhone 3G / 3G / 3GS)
640 x 960 (for iPhone 4 / 4S / 5)


iOS/iPhone Background Colors and Background Image Example

ไฟล์รูปภาพที่จะนำไปใช้

iOS/iPhone Background Colors and Background Image Example

ให้ทำการ Copy ไปไว้ในโฟเดอร์ของ Project

iOS/iPhone Background Colors and Background Image Example

ทำการ Add ไฟล์เข้ามาใน Project โดยการคลิกขวาเลือก Add Fields to...

iOS/iPhone Background Colors and Background Image Example

เลือกไฟล์รุปภาพที่อยู่ใน Project

iOS/iPhone Background Colors and Background Image Example

ไฟล์ที่ได้

iOS/iPhone Background Colors and Background Image Example

ในการเขียนเพื่อแสดง Background Image สามารถแทรกได้ที่ method ของ viewDidLoad ได้ในทันที








- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
}


Screenshot

iOS/iPhone Background Colors and Background Image Example

แสดง Background image บน iOS iPhone Simulator

เราจะมาสร้าง Button สำหรับเปลี่ยน Background Image แบบง่าย ๆ

iOS/iPhone Background Colors and Background Image Example

สร้าง Button ขึ้นมา 3 ตัว ดังรูป

iOS/iPhone Background Colors and Background Image Example

ทำการเชื่อม IBOutlet และ IBAction บน Class ของ .m

iOS/iPhone Background Colors and Background Image Example

ใน Class ของ .m เราจะได้ method ดังรูป

iOS/iPhone Background Colors and Background Image Example

ให้ใส่คำสั่งสำหรับเปลี่ยน Background แบบง่าย ๆ

- (IBAction)Bg1Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
}

- (IBAction)Bg2Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.jpg"]];
}

- (IBAction)Bg3Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg3.jpg"]];
}



Screenshot

iOS/iPhone Background Colors and Background Image Example

แสดง Background Image เมื่อคลิกเปลี่ยน Button

iOS/iPhone Background Colors and Background Image Example

แสดง Background Image เมื่อคลิกเปลี่ยน Button

iOS/iPhone Background Colors and Background Image Example

แสดง Background Image เมื่อคลิกเปลี่ยน Button

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

ViewController.h
//
//  ViewController.h
//  backgroundApp
//
//  Created by Weerachai on 11/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


- (IBAction)Bg1Changed:(id)sender;

- (IBAction)Bg2Changed:(id)sender;

- (IBAction)Bg3Changed:(id)sender;

@end




ViewController.m
//
//  ViewController.m
//  backgroundApp
//
//  Created by Weerachai on 11/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //self.view.backgroundColor = [UIColor redColor];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Bg1Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
}

- (IBAction)Bg2Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.jpg"]];
}

- (IBAction)Bg3Changed:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg3.jpg"]];
}


- (void)dealloc {
    [super dealloc];
}

@end


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-13 16:46:38 / 2017-03-26 00:19:04
  Download : Download  iOS/iPhone Background Colors and Background Image Example (iPhone,iPad)
 Sponsored Links / Related

 
iOS/iPhone Show Image and Display Picture - UIImage (Objective-C,iPhone)
Rating :

 
iOS/iPhone Set Image Alpha/Opacity with Slider (UIImageView and UISlider)
Rating :

 
iOS/iPhone Page Control and Image View (UIImageView) (Objective-C, iPhone, iPad)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่