我怎麼就能對齊正常的UIButton如。以編程方式在頂部,底部或中間?如何以編程方式對齊iOS應用程序中的按鈕?
回答
可以設置按鈕的中心將其置於相應
yourButton.center = CGPointMake(0.0, 0.0);// for topleft
yourButton.center = CGPointMake(160.0, 240.0);// for center
yourButton.center = CGPointMake(320.0, 480.0);// for bottomright
希望這會有幫助
但是,這並不能補償按鈕的大小。例如,在第一行中,您的按鈕將部分隱藏。 – woz
其實這傢伙想要對齊按鈕(頂部,底部或中間),而不是設置框架,因此我給出了我的答案 –
320和480是否也是iPhone 5或5S的有效指標? –
您應該設置它的框架。
喜歡的東西
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];
或
button.frame = CGRectMake(320, 120, 35, 10);
請注意,第一對值分別是視圖和第二的寬度和高度(位置在X和Y)值的一對是按鈕本身的寬度和高度。
所以,如果你想在頂部的按鈕,你應該輸入
button.frame = CGRectMake(0, 0, 35, 10);
中東
button.frame = CGRectMake(160, 240, 35, 10);
底部
button.frame = CGRectMake(320, 480, 35, 10);
設置子視圖的位置的唯一途徑是手動的。您可以設置框架,這樣,例如:
或改變原產地,像這樣的,例如:
view.frame.origin.x = INT_VALUE;
獲得某種比對的唯一途徑是與self.view.frame
做數學。如果你想與屏幕的中間水平對齊對象,請使用:如果要對齊東西頂用的44個像素的邊距
view.frame.origin.x = self.view.frame.size.width/2
,使用此:
view.frame.origin.y = self.view.frame.origin.y = (view.frame.size.height/2) + 44;
等等
我喜歡你回答的絕對性。 「唯一的方法」xD –
你必須設置UIButton
構架你的自我。
平鋪對齊
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
[button setFrame:CGRectMake(X_Co, yourYposition, yourButtonWidth, yourButtonheight)];
垂直對齊
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(yourXposition, Y_Co, yourButtonWidth, yourButtonheight)];
左上
[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)];
右上方
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
左下角
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
右下角
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
中心self.view中
button.center = self.view.center;
OR
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
@TheTiger的偉大提示! 我把這個邏輯在迅速類:
https://github.com/iurims/iSLibs/blob/master/ISPositionCalculator.swift
如果您的代碼片段包含您的答案,即使該GitHub回購消失(或變爲私人,或文件名稱更改,或...),它仍然有用。 – kaveman
- 1. 以編程方式設置按鈕對齊方式
- 2. 如何在wp8應用程序中以編程方式對齊輪轂瓷磚
- 3. 如何以編程方式設置UIButton的垂直對齊 - iOS
- 4. 如何以編程方式通過Facebook應用程序「按」一個「贊」按鈕?
- 5. iOS:以編程方式對齊屏幕底部的按鈕x像素
- 6. 以編程方式將按鈕添加到UWP應用程序
- 7. 以編程方式從手機中的應用程序中按一個按鈕
- 8. 如何在Android應用程序中對齊LinearLayout中的按鈕?
- 9. 對齊應用程序欄按鈕
- 10. 如何以編程方式安裝iOS應用程序?
- 11. 如何以編程方式創建iOS應用程序視圖
- 12. 如何以編程方式將Stoplight按鈕添加到OS X應用程序
- 13. 如何以編程方式通過按鈕單擊退出應用程序
- 14. Xamarin iOS UIButton如何以編程方式單擊按鈕?
- 15. 如何使按鈕以編程方式按順序閃爍
- 16. 如何以絕對佈局以編程方式移動按鈕
- 17. 以編程方式在IOS中安裝企業應用程序
- 18. 以編程方式單擊Windows窗體應用程序中的網頁按鈕
- 19. 以編程方式禁用按鈕
- 20. 如何以編程方式「跟隨」iOS應用程序中的推文Feed?
- 21. 如何以編程方式重新啓動iOS中的iPhone應用程序
- 22. 如何以編程方式「喜歡」iOS應用程序中的Facebook頁面?
- 23. 以編程方式居中按鈕
- 24. 如何以編程方式調用UINavigationBar中的後退按鈕?
- 25. 如何使用swift以編程方式將應用程序帶到ios 9中?
- 26. 如何以編程方式卸載Windows中的應用程序?
- 27. 以編程方式按下按鈕
- 28. iPhone:以編程方式按下按鈕
- 29. 以編程方式按下此按鈕
- 30. 以編程方式編寫android:按鈕
你可以通過設置freame對齊按鈕。 – HarshIT