1
我有定製的UIButton,它是視圖的高度/寬度的1/4。我想要做的是在按鈕標題上方設置一個圖片,並將它們都置於所述按鈕中央。UIButton標題上方的中心圖像
代碼:
CGRect imageFrame= CGRectMake(0, 0, 80, 80);
UIEdgeInsets imageInset = UIEdgeInsetsMake(0, 0, 30, 0);
UIEdgeInsets titleInset = UIEdgeInsetsMake(80, 0, 0, 0);
btnSideSettings = [UIButton buttonWithType:UIButtonTypeCustom];
btnSideSettings.frame = settingsFrame;
[btnSideSettings setImage:[UIImage imageNamed:@"btnSideSettings"] forState:UIControlStateNormal];
btnSideSettings.imageView.frame = imageFrame;
btnSideSettings.imageEdgeInsets = imageInset;
btnSideSettings.titleEdgeInsets = titleInset;
btnSideSettings.titleLabel.textAlignment = NSTextAlignmentCenter;
[btnSideSettings setTitle:@"Settings" forState:UIControlStateNormal];
結果:
我試過上面的代碼的幾個變種,主要是編輯插圖。如果我評論添加圖片,標題也會居中。感謝任何幫助。
btnSideSettings.frame = settingsFrame; ?? – mindfreak